From 35b6cbcc46b876e074b6fba882606e121e8e56db Mon Sep 17 00:00:00 2001 From: qibaoguang Date: Tue, 10 Feb 2015 23:54:45 +0800 Subject: [PATCH] Update 26.3.3. Customizing embedded servlet containers.md --- ...Customizing embedded servlet containers.md | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/IV. Spring Boot features/26.3.3. Customizing embedded servlet containers.md b/IV. Spring Boot features/26.3.3. Customizing embedded servlet containers.md index 4a3722c..3d1bb22 100644 --- a/IV. Spring Boot features/26.3.3. Customizing embedded servlet containers.md +++ b/IV. Spring Boot features/26.3.3. Customizing embedded servlet containers.md @@ -7,4 +7,34 @@ 2. server.address - 绑定的接口地址 3. server.sessionTimeout - session超时时间 -具体参考[ServerProperties](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java) +具体参考[ServerProperties](http://github.com/spring-projects/spring-boot/tree/master/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java)。 + +* 编程方式的自定义 + +如果需要以编程的方式配置内嵌的servlet容器,你可以注册一个实现EmbeddedServletContainerCustomizer接口的Spring bean。EmbeddedServletContainerCustomizer提供对ConfigurableEmbeddedServletContainer的访问,ConfigurableEmbeddedServletContainer包含很多自定义的setter方法。 +```java +import org.springframework.boot.context.embedded.*; +import org.springframework.stereotype.Component; + +@Component +public class CustomizationBean implements EmbeddedServletContainerCustomizer { + @Override + public void customize(ConfigurableEmbeddedServletContainer container) { + container.setPort(9000); + } +} +``` +* 直接自定义ConfigurableEmbeddedServletContainer + +如果上面的自定义手法过于受限,你可以自己注册TomcatEmbeddedServletContainerFactory,JettyEmbeddedServletContainerFactory或UndertowEmbeddedServletContainerFactory。 +```java +@Bean +public EmbeddedServletContainerFactory servletContainer() { + TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); + factory.setPort(9000); + factory.setSessionTimeout(10, TimeUnit.MINUTES); + factory.addErrorPages(new ErrorPage(HttpStatus.NOT_FOUND, "/notfound.html"); + return factory; +} +``` +很多可选的配置都提供了setter方法,也提供了一些受保护的钩子方法以满足你的某些特殊需求。具体参考相关文档。