Commit 699d083c authored by Stephane Nicoll's avatar Stephane Nicoll

Add load-on-startup property to DispatcherServlet

Closes gh-2481
parent dc353674
...@@ -110,6 +110,8 @@ public class DispatcherServletAutoConfiguration { ...@@ -110,6 +110,8 @@ public class DispatcherServletAutoConfiguration {
ServletRegistrationBean registration = new ServletRegistrationBean( ServletRegistrationBean registration = new ServletRegistrationBean(
dispatcherServlet(), this.server.getServletMapping()); dispatcherServlet(), this.server.getServletMapping());
registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
registration.setLoadOnStartup(
this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) { if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig); registration.setMultipartConfig(this.multipartConfig);
} }
......
...@@ -84,6 +84,8 @@ public class WebMvcProperties { ...@@ -84,6 +84,8 @@ public class WebMvcProperties {
private final Async async = new Async(); private final Async async = new Async();
private final Servlet servlet = new Servlet();
private final View view = new View(); private final View view = new View();
public DefaultMessageCodesResolver.Format getMessageCodesResolverFormat() { public DefaultMessageCodesResolver.Format getMessageCodesResolverFormat() {
...@@ -164,6 +166,10 @@ public class WebMvcProperties { ...@@ -164,6 +166,10 @@ public class WebMvcProperties {
return this.async; return this.async;
} }
public Servlet getServlet() {
return this.servlet;
}
public View getView() { public View getView() {
return this.view; return this.view;
} }
...@@ -187,6 +193,23 @@ public class WebMvcProperties { ...@@ -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 { public static class View {
/** /**
......
...@@ -22,6 +22,7 @@ import javax.servlet.http.HttpServletRequest; ...@@ -22,6 +22,7 @@ import javax.servlet.http.HttpServletRequest;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.UnsatisfiedDependencyException; import org.springframework.beans.factory.UnsatisfiedDependencyException;
import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.boot.web.servlet.MultipartConfigFactory;
...@@ -151,17 +152,19 @@ public class DispatcherServletAutoConfigurationTests { ...@@ -151,17 +152,19 @@ public class DispatcherServletAutoConfigurationTests {
this.context.register(ServerPropertiesAutoConfiguration.class, this.context.register(ServerPropertiesAutoConfiguration.class,
DispatcherServletAutoConfiguration.class); DispatcherServletAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"spring.mvc.throw-exception-if-no-handler-found:true"); "spring.mvc.throw-exception-if-no-handler-found:true",
EnvironmentTestUtils.addEnvironment(this.context, "spring.mvc.dispatch-options-request:true",
"spring.mvc.dispatch-options-request:true"); "spring.mvc.dispatch-trace-request:true",
EnvironmentTestUtils.addEnvironment(this.context, "spring.mvc.servlet.load-on-startup=5");
"spring.mvc.dispatch-trace-request:true");
this.context.refresh(); this.context.refresh();
DispatcherServlet bean = this.context.getBean(DispatcherServlet.class); DispatcherServlet bean = this.context.getBean(DispatcherServlet.class);
assertThat(bean).extracting("throwExceptionIfNoHandlerFound") assertThat(bean).extracting("throwExceptionIfNoHandlerFound")
.containsExactly(true); .containsExactly(true);
assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(true); assertThat(bean).extracting("dispatchOptionsRequest").containsExactly(true);
assertThat(bean).extracting("dispatchTraceRequest").containsExactly(true); assertThat(bean).extracting("dispatchTraceRequest").containsExactly(true);
assertThat(
new DirectFieldAccessor(this.context.getBean("dispatcherServletRegistration"))
.getPropertyValue("loadOnStartup")).isEqualTo(5);
} }
@Configuration @Configuration
......
...@@ -337,6 +337,7 @@ content into your application; rather pick only the properties that you need. ...@@ -337,6 +337,7 @@ content into your application; rather pick only the properties that you need.
spring.mvc.locale= # Locale to use. spring.mvc.locale= # Locale to use.
spring.mvc.media-types.*= # Maps file extensions to media types for content negotiation. 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.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.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.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. spring.mvc.view.prefix= # Spring MVC view prefix.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment