From 699d083ceca06d6f2afbe2c69c35bfc90c8f222d Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Tue, 10 May 2016 08:37:23 +0200 Subject: [PATCH] Add load-on-startup property to DispatcherServlet Closes gh-2481 --- .../DispatcherServletAutoConfiguration.java | 2 ++ .../autoconfigure/web/WebMvcProperties.java | 23 +++++++++++++++++++ ...spatcherServletAutoConfigurationTests.java | 13 +++++++---- .../appendix-application-properties.adoc | 1 + 4 files changed, 34 insertions(+), 5 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java index a245b914fb..cc82f2331a 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration.java @@ -110,6 +110,8 @@ public class DispatcherServletAutoConfiguration { ServletRegistrationBean registration = new ServletRegistrationBean( dispatcherServlet(), this.server.getServletMapping()); registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); + registration.setLoadOnStartup( + this.webMvcProperties.getServlet().getLoadOnStartup()); if (this.multipartConfig != null) { registration.setMultipartConfig(this.multipartConfig); } diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java index ac3af5d8d1..0ea3895548 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcProperties.java @@ -84,6 +84,8 @@ public class WebMvcProperties { private final Async async = new Async(); + private final Servlet servlet = new Servlet(); + private final View view = new View(); public DefaultMessageCodesResolver.Format getMessageCodesResolverFormat() { @@ -164,6 +166,10 @@ public class WebMvcProperties { return this.async; } + public Servlet getServlet() { + return this.servlet; + } + public View getView() { return this.view; } @@ -187,6 +193,23 @@ public class WebMvcProperties { } + public static class Servlet { + + /** + * Load on startup priority of the dispatcher servlet. + */ + private int loadOnStartup = -1; + + public int getLoadOnStartup() { + return this.loadOnStartup; + } + + public void setLoadOnStartup(int loadOnStartup) { + this.loadOnStartup = loadOnStartup; + } + + } + public static class View { /** diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfigurationTests.java index b296325bbe..545d996288 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfigurationTests.java @@ -22,6 +22,7 @@ import javax.servlet.http.HttpServletRequest; import org.junit.After; import org.junit.Test; +import org.springframework.beans.DirectFieldAccessor; import org.springframework.beans.factory.UnsatisfiedDependencyException; import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.web.servlet.MultipartConfigFactory; @@ -151,17 +152,19 @@ public class DispatcherServletAutoConfigurationTests { this.context.register(ServerPropertiesAutoConfiguration.class, DispatcherServletAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, - "spring.mvc.throw-exception-if-no-handler-found:true"); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.mvc.dispatch-options-request:true"); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.mvc.dispatch-trace-request:true"); + "spring.mvc.throw-exception-if-no-handler-found:true", + "spring.mvc.dispatch-options-request:true", + "spring.mvc.dispatch-trace-request:true", + "spring.mvc.servlet.load-on-startup=5"); this.context.refresh(); DispatcherServlet bean = this.context.getBean(DispatcherServlet.class); assertThat(bean).extracting("throwExceptionIfNoHandlerFound") .containsExactly(true); assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(true); assertThat(bean).extracting("dispatchTraceRequest").containsExactly(true); + assertThat( + new DirectFieldAccessor(this.context.getBean("dispatcherServletRegistration")) + .getPropertyValue("loadOnStartup")).isEqualTo(5); } @Configuration diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index 8fa86af8e2..8fc93afbb5 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -337,6 +337,7 @@ content into your application; rather pick only the properties that you need. spring.mvc.locale= # Locale to use. spring.mvc.media-types.*= # Maps file extensions to media types for content negotiation. spring.mvc.message-codes-resolver-format= # Formatting strategy for message codes. For instance `PREFIX_ERROR_CODE`. + spring.mvc.servlet.load-on-startup=-1 # Load on startup priority of the Spring Web Services servlet. spring.mvc.static-path-pattern=/** # Path pattern used for static resources. spring.mvc.throw-exception-if-no-handler-found=false # If a "NoHandlerFoundException" should be thrown if no Handler was found to process a request. spring.mvc.view.prefix= # Spring MVC view prefix.