From e2944c37e4097eafddd61a20038bff1a6e9c4097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Deleuze?= Date: Tue, 16 Jun 2020 15:55:41 +0200 Subject: [PATCH] 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 --- .../web/servlet/DispatcherServlet.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java index b1d9f85242..3a11cf8c78 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/DispatcherServlet.java @@ -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 List getDefaultStrategies(ApplicationContext context, Class 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) {