配置嵌入式Servlet容器
- Servlet默认使用Tomcat作为嵌入式的Servlet容器
- 如何定制和修改Servlet容器的相关配置?
- 修改和Server有关的配置(ServerProperties, 其实也是一个WebServerFactoryCustomizer)
server.port=8081 server.context-path=/xxx server.tomcat.uri-encoding=UTF-8 //通用的Servlet容器设置 server.xxx //Tomcat的设置 server.tomcat.xxx
- 编写一个WebServerFactoryCustomizer<ConfigurableWebServerFactory>: 嵌入式的Servlet容器的定制器, 来修改Servlet容器的配置, (比起1, 优先级更高)
@Bean public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer(){ return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
//定制嵌入式的Servlet容器相关的规则 @Override public void customize(ConfigurableWebServerFactory factory) { factory.setPort(8081); } }; }
- 修改和Server有关的配置(ServerProperties, 其实也是一个WebServerFactoryCustomizer)
- 注册Servlet, Filter, Listener三大组件.
- 由于SpringBoot默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用, 没有web.xml文件, 所以注册三大组件用以下方式.
- ServletRegistrationBean
@Configuration public class MyServerConfig { //注册三大组件 @Bean public ServletRegistrationBean myServlet() { ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyServlet(), "/myServlet"); servletRegistrationBean.setLoadOnStartup(1); return servletRegistrationBean; } }
- FilterRegistrationBean
@Bean public FilterRegistrationBean myFilter() { FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(new MyFilter()); registrationBean.setUrlPatterns(Arrays.asList("/hello", "/myServlet")); return registrationBean; }
- ServletListenerRegistrationBean
@Bean public ServletListenerRegistrationBean myListener() { ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener()); return registrationBean; }
- 举个例子: SpringBoot再帮我们自动配置SpringMVC时, 自动注册前端控制器DispatcherServlet, 其中DispatcherServletAutoConfiguration
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME) @ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME) public ServletRegistrationBean dispatcherServletRegistration( DispatcherServlet dispatcherServlet) { ServletRegistrationBean registration = new ServletRegistrationBean( dispatcherServlet, this.serverProperties.getServletMapping());
//默认拦截: / 所有请求;包静态资源,但是不拦截jsp请求; /*会拦截jsp //可以通过server.servletPath来修改SpringMVC前端控制器默认拦截的请求路径 registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME); registration.setLoadOnStartup( this.webMvcProperties.getServlet().getLoadOnStartup()); if (this.multipartConfig != null) { registration.setMultipartConfig(this.multipartConfig); } return registration; }
- 使用其他的Servlet容器
- Tomcat(默认使用), 先排除掉
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <artifactId>spring-boot-starter-tomcat</artifactId> <groupId>org.springframework.boot</groupId> </exclusion> </exclusions> </dependency>
- Undertow(不支持Jsp, 但并发处理能力很好).
<dependency> <artifactId>spring-boot-starter-undertow</artifactId> <groupId>org.springframework.boot</groupId> </dependency>
- Jetty(长连接)
<dependency> <artifactId>spring-boot-starter-jetty</artifactId> <groupId>org.springframework.boot</groupId> </dependency>
- Tomcat(默认使用), 先排除掉
使用外置的Servlet容器
- 嵌入式Servlet容器: 打jar包
- 优点: 简单便捷.
- 缺点: 默认不支持Jsp, 优化定制Servlet容器很复杂(定制器, 自己编写嵌入式Servlet容器的创建工厂).
- 外置的Servlet容器: 外面安装Tomcat, 应用war包的方式打包.
- 步骤
- 必须创建一个war项目: 利用idea创建好目录结构
- 将嵌入式的Tomcat指定为provided
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency>
- 必须编写一个SpringBootServletInitializer, 并且调用configure方法
public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(SpringBoot04WebWarApplication.class); } }
- 启动服务器