EmbeddedServletContainerCustomizer class does not exist at the above configuration Boot2.0 embedded Servlet Container Spring, via a network discovery query is replaced WebServerFactoryCustomizer.
Spring Boot 1.0中:
General Configuration Example
@Component public static class CustomServletContainer the implements EmbeddedServletContainerCustomizer { @Override public void Customize (ConfigurableEmbeddedServletContainer Container) { container.setPort ( 8888 ); // Configure Port container.addErrorPages ( new new the ErrorPage (HttpStatus.NOT_FOUND, " /404.html " )); // configuration error page container.setSessionTimeout ( 10 , TimeUnit.MINUTES); // configure user session expiration time } }
Examples particular configuration
以Tomcat为例。 @Bean public EmbeddedServletContainerFactory servletContainer() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); factory.setPort(8888); factory.setSessionTimeout(10, TimeUnit.MINUTES); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/404.html")); return factory; }
After springboot2.0
@FunctionalInterface public interface WebServerFactoryCustomizer<T extends WebServerFactory> { void customize(T factory); }
package springboot.configure; import org.springframework.boot.web.server.ConfigurableWebServerFactory; import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.WebServerFactoryCustomizer; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; @Component public class CustomServletContainer implements WebServerFactoryCustomizer<ConfigurableWebServerFactory>{ @Override public void customize(ConfigurableWebServerFactory factory) { factory.setPort(8888); factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND,"/404.html")); } }
@Bean public ConfigurableServletWebServerFactory webServerFactory () { TomcatServletWebServerFactory Factory = new new TomcatServletWebServerFactory (); factory.setPort ( 9000 ); factory.setUriEncoding (); // only set session, session expiration time can only be configured in the configuration file return Factory; }