Skip to content

Commit

Permalink
Make DispatcherServlet.properties loading lazy
Browse files Browse the repository at this point in the history
With spring-projects#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 spring-projectsgh-25257
  • Loading branch information
sdeleuze authored and kenny5he committed Jun 21, 2020
1 parent a4787f4 commit 807afc8
Showing 1 changed file with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
* @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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -869,6 +859,19 @@ protected <T> T getDefaultStrategy(ApplicationContext context, Class<T> strategy
*/
@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) {
Expand Down

0 comments on commit 807afc8

Please sign in to comment.