From a74b86e8124c3012e54481b70ffeddbc7bb357a9 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Tue, 31 Jan 2023 16:05:07 +0100 Subject: [PATCH] Lazily load ContextLoader.properties (and lazily fail if not present) Closes gh-29905 --- .../web/context/ContextLoader.java | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/context/ContextLoader.java b/spring-web/src/main/java/org/springframework/web/context/ContextLoader.java index 55b7be8774b..1f5c6f42de7 100644 --- a/spring-web/src/main/java/org/springframework/web/context/ContextLoader.java +++ b/spring-web/src/main/java/org/springframework/web/context/ContextLoader.java @@ -132,22 +132,6 @@ public class ContextLoader { private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties"; - private static final Properties defaultStrategies; - - static { - // Load default strategy implementations from properties file. - // This is currently strictly internal and not meant to be customized - // by application developers. - try { - ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class); - defaultStrategies = PropertiesLoaderUtils.loadProperties(resource); - } - catch (IOException ex) { - throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage()); - } - } - - /** * Map from (thread context) ClassLoader to corresponding 'current' WebApplicationContext. */ @@ -161,6 +145,8 @@ public class ContextLoader { @Nullable private static volatile WebApplicationContext currentContext; + @Nullable + private static Properties defaultStrategies; /** * The root WebApplicationContext instance that this loader manages. @@ -352,6 +338,18 @@ public class ContextLoader { } } else { + if (defaultStrategies == null) { + // Load default strategy implementations from properties file. + // This is currently strictly internal and not meant to be customized + // by application developers. + try { + ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, ContextLoader.class); + defaultStrategies = PropertiesLoaderUtils.loadProperties(resource); + } + catch (IOException ex) { + throw new IllegalStateException("Could not load 'ContextLoader.properties': " + ex.getMessage()); + } + } contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName()); try { return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());