springboot嵌入式Servlet容器修改

Springboot默认Servlet容器

从springboot的依赖图可以看出,springboot默认使用tomcat作为Servlet容器

定制修改Servlet容器配置

修改和Server相关的配置(ServerProperties)

  • 修改ServerProperties的相关参数
1
2
3
4
5
6
7
server:
port: 8081
servlet:
context-path: /example
# 配置tomcat
tomcat:
uri-encoding: UTF-8
  • 编码方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    /**
    * 定制修改Servlet容器配置代码方式
    */

    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer (){
    return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
    factory.setPort(8083);
    }


    };
    }

注册Servlet,Listener,Filter

  • ServletRegistrationBean

    在以前我们注册一个servlet需要在web.xml中注册,但现在springboot打包成功为一个jar包,使用嵌入式tomcat。为此springboot为我们提供了一个注册servlet的类即ServletRegistrationBean

    我们只需要在容器中注入ServletRegistrationBean,调用他的API即可注册servlet

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    /**
    * 注册Servlet
    * @return
    */
    @Bean
    public ServletRegistrationBean servletRegistrationBean(){
    ServletRegistrationBean<Servlet> servletServletRegistrationBean = new ServletRegistrationBean<>();
    servletServletRegistrationBean.setServlet(new MyServlet());
    servletServletRegistrationBean.setUrlMappings(Arrays.asList("/helloServlet"));

    return servletServletRegistrationBean;
    }
  • ServletListenerRegistrationBean

    注册Listener也类似

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    /**
    * 注册LIstener
    */

    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
    ServletListenerRegistrationBean<MyListener> eventListenerServletListenerRegistrationBean = new ServletListenerRegistrationBean<>();
    eventListenerServletListenerRegistrationBean.setListener(new MyListener());
    return eventListenerServletListenerRegistrationBean;

    }
  • FilterRegistrationBean

    注册filter也类似

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    /**
    * 注册Filter
    */
    @Bean
    public FilterRegistrationBean filterRegistrationBean(){
    FilterRegistrationBean<Filter> filterFilterRegistrationBean = new FilterRegistrationBean<>();
    filterFilterRegistrationBean.setFilter(new MyFilter());

    return filterFilterRegistrationBean;

    }

Springboot帮我们在自动配置SpringMVC时,会自动注册SpringMVC的前端控制器;DispatcherServlet

在DispatcherServletAutoConfiguration中注册了所有的registrationBean

1
2
3
4
5
6
7
8
9
10
11
12
@Bean(name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME)
@ConditionalOnBean(value = DispatcherServlet.class, name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
public DispatcherServletRegistrationBean dispatcherServletRegistration(DispatcherServlet dispatcherServlet) {
DispatcherServletRegistrationBean registration = new DispatcherServletRegistrationBean(dispatcherServlet,
this.webMvcProperties.getServlet().getPath());
registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
registration.setLoadOnStartup(this.webMvcProperties.getServlet().getLoadOnStartup());
if (this.multipartConfig != null) {
registration.setMultipartConfig(this.multipartConfig);
}
return registration;
}

默认拦截/

更换其他Servlet容器

将spring-boot-starter-web中的默认tomcat-starter去除,添加自己需要的容器starter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>

</exclusions>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>