Make DispatcherServlet.properties loading lazy

With #25209, DispatcherServlet.properties loading and parsing
will be useless for most of use cases, and it requires
configuration on GraalVM native images.

The purpose of this issue to make such loading and parsing lazy,
only invoked in getDefaultStrategies() if needed.

Closes gh-25257
This commit is contained in:
Sébastien Deleuze 2020-06-16 15:55:41 +02:00
parent 2a74eff10f
commit e2944c37e4
1 changed files with 17 additions and 14 deletions

View File

@ -157,6 +157,7 @@ import org.springframework.web.util.WebUtils;
* @author Rob Harrop
* @author Chris Beams
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
* @see org.springframework.web.HttpRequestHandler
* @see org.springframework.web.servlet.mvc.Controller
* @see org.springframework.web.context.ContextLoaderListener
@ -280,20 +281,9 @@ public class DispatcherServlet extends FrameworkServlet {
/** Additional logger to use when no mapped handler is found for a request. */
protected static final Log pageNotFoundLogger = LogFactory.getLog(PAGE_NOT_FOUND_LOG_CATEGORY);
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, DispatcherServlet.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load '" + DEFAULT_STRATEGIES_PATH + "': " + ex.getMessage());
}
}
/** Store default strategy implementations. */
@Nullable
private static Properties defaultStrategies;
/** Detect all HandlerMappings or just expect "handlerMapping" bean?. */
private boolean detectAllHandlerMappings = true;
@ -869,6 +859,19 @@ public class DispatcherServlet extends FrameworkServlet {
*/
@SuppressWarnings("unchecked")
protected <T> List<T> getDefaultStrategies(ApplicationContext context, Class<T> strategyInterface) {
if (defaultStrategies == null) {
try {
// Load default strategy implementations from properties file.
// This is currently strictly internal and not meant to be customized
// by application developers.
ClassPathResource resource = new ClassPathResource(DEFAULT_STRATEGIES_PATH, DispatcherServlet.class);
defaultStrategies = PropertiesLoaderUtils.loadProperties(resource);
}
catch (IOException ex) {
throw new IllegalStateException("Could not load '" + DEFAULT_STRATEGIES_PATH + "': " + ex.getMessage());
}
}
String key = strategyInterface.getName();
String value = defaultStrategies.getProperty(key);
if (value != null) {