Lazily load ContextLoader.properties (and lazily fail if not present)

Closes gh-29905
This commit is contained in:
Juergen Hoeller 2023-01-31 16:05:07 +01:00
parent 7c9dca3d2e
commit a74b86e812
1 changed files with 14 additions and 16 deletions

View File

@ -132,22 +132,6 @@ public class ContextLoader {
private static final String DEFAULT_STRATEGIES_PATH = "ContextLoader.properties"; 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. * Map from (thread context) ClassLoader to corresponding 'current' WebApplicationContext.
*/ */
@ -161,6 +145,8 @@ public class ContextLoader {
@Nullable @Nullable
private static volatile WebApplicationContext currentContext; private static volatile WebApplicationContext currentContext;
@Nullable
private static Properties defaultStrategies;
/** /**
* The root WebApplicationContext instance that this loader manages. * The root WebApplicationContext instance that this loader manages.
@ -352,6 +338,18 @@ public class ContextLoader {
} }
} }
else { 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()); contextClassName = defaultStrategies.getProperty(WebApplicationContext.class.getName());
try { try {
return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader()); return ClassUtils.forName(contextClassName, ContextLoader.class.getClassLoader());