SpringBoot之配置嵌入式Servlet容器

1、SpringBoot默認使用Tomcat作爲嵌入式的Servlet容器;

2、如何定製和修改Servlet容器的相關配置;有兩種方式

    1).修改和server有關的配置(ServerProperties【也是EmbeddedServletContainerCustomizer】);

spring.mvc.date-format=yyyy-MM-dd
spring.thymeleaf.cache=false 
spring.messages.basename=i18n.login

server.port=8081
server.context-path=/crud
server.tomcat.uri-encoding=UTF-8

通用的Servlet容器設置server.xxx    Tomcat的設置server.tomcat.xxx

    2).編寫一個**EmbeddedServletContainerCustomizer**:嵌入式的Servlet容器的定製器;來修改Servlet容器的配置

3、註冊servlet三大組件:servlet,filter,listener 

MyServlet:

MyFilter:過濾器

MyListener:監聽器

由於SpringBoot默認是以jar包的方式啓動嵌入式的Servlet容器來啓動SpringBoot的web應用,沒有web.xml文件。註冊三大組件用以下方式ServletRegistrationBean,FilterRegistrationBean,ServletListenerRegistrationBean

@Configuration
public class MyServerConfig {
   //註冊三大組件
    @Bean
    public ServletRegistrationBean myServlet(){
        //映射/myServlet請求
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }
    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return registrationBean;
    }
    @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }
    //配置嵌入式的Servlet容器
    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
        return new EmbeddedServletContainerCustomizer() {
            //定製嵌入式的Servlet容器相關的規則
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                container.setPort(8083);
            }
        };
    }

}

SpringBoot在配置SpringMVC的時候,自動的註冊SpringMVC的前端控制器:DIspatcherServlet;所以不用配了

參考DispatcherServletAutoConfiguration類源碼:

4、替換爲其他嵌入式Servlet容器

默認爲tomcat:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
   引入web模塊默認就是使用嵌入式的Tomcat作爲Servlet容器;
</dependency>

Jetty:

<!-- 引入web模塊 -->
<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>

<!--引入其他的Servlet容器-->
<dependency>
   <artifactId>spring-boot-starter-jetty</artifactId>
   <groupId>org.springframework.boot</groupId>
</dependency>

Undertow:

<!-- 引入web模塊 -->
<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>

<!--引入其他的Servlet容器-->
<dependency>
   <artifactId>spring-boot-starter-undertow</artifactId>
   <groupId>org.springframework.boot</groupId>
</dependency>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章