【spring boot】第8篇:spring boot 中的 servlet 容器
嵌入式 servlet 容器
在 spring boot 之前的web開發,我們都是把我們的應用部署到 Tomcat 等servelt容器,這些容器一般都會在我們的應用伺服器上安裝好環境,但是 spring boot 中並不需要外部應用伺服器安裝這些servlet容器,spring boot自帶了嵌入式的servlet容器。
如何修改和定製嵌入式servlet容器
- 在application.yaml檔案中配置修改
#修改服務埠號 server.port=8081 #配置統一請求路徑 server.context‐path=/crud #配置tomcat相關 server.tomcat.uri‐encoding=UTF‐8
這些配置相關的屬性都定義在org.springframework.boot.autoconfigure.web.ServerProperties
類中
- 編寫一個嵌入式的servlet容器的定製器來修改相關的配置
@Component public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> { @Override public void customize(ConfigurableServletWebServerFactory server) { server.setPort(9000); } }
- 直接定製具體的servlet容器配置,比如 tomcat 容器
@Configuration public class ApplicationConfig implements WebMvcConfigurer { @Bean public ConfigurableServletWebServerFactory configurableServletWebServerFactory() { TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory(); factory.setPort(8585); return factory; } }