SpringBoot- configuration Embedded Servlet container (13)

SpringBoot use as an embedded Tomcat Servlet container default;

How to customize and modify Servlet container configuration

1, modify and server-related configuration (ServerProperties EmbeddedServletContainerCustomizer [also]);

= 8081 the server.port 
server.context -path = / CRUD 

server.tomcat.uri -encoding. 8 = UTF- // generic Servlet container provided server.xxx
 // Set the Tomcat 
server.tomcat.xxx


2, write a EmbeddedServletContainerCustomizer : Servlet container custom embedded device; to modify the configuration Servlet container

@Bean   // Be sure to customize this is added to the container 
public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer () {
     return  new new EmbeddedServletContainerCustomizer () { 

        // custom embedded Servlet container-related rules 
        @Override
         public  void Customize (ConfigurableEmbeddedServletContainer Container) { 
            container.setPort ( 8083 ); 
        } 
    }; 
}

Registration Servlet three components [Servlet, Filter, Listener]

Since SpringBoot default jar package is the way to start the embedded Servlet container to start web application SpringBoot no web.xml file.

Registered three components in the following way

ServletRegistrationBean

//注册三大组件
@Bean
public ServletRegistrationBean myServlet(){
    ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
    return registrationBean;
}

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 help us SpringMVC automatically when the auto-registration SpringMVC front controller; DIspatcherServlet;

DispatcherServletAutoConfiguration中:

@Bean (name = DEFAULT_DISPATCHER_SERVLET_REGISTRATION_BEAN_NAME) 
@ConditionalOnBean (value . = The DispatcherServlet class , name = DEFAULT_DISPATCHER_SERVLET_BEAN_NAME)
 public ServletRegistrationBean dispatcherServletRegistration ( 
      the DispatcherServlet DispatcherServlet) { 
   ServletRegistrationBean Registration = new new ServletRegistrationBean ( 
         DispatcherServlet, the this .serverProperties.getServletMapping ());
     // default intercept: / all requests; static packet resources, but does not intercept requests jsp; / * intercepts jsp
     // can modify the default SpringMVC front controller request path intercepted server.servletPath
    
   registration.setName(DEFAULT_DISPATCHER_SERVLET_BEAN_NAME);
   registration.setLoadOnStartup(
         this.webMvcProperties.getServlet().getLoadOnStartup());
   if (this.multipartConfig != null) {
      registration.setMultipartConfig(this.multipartConfig);
   }
   return registration;
}

Replace other embedded Servlet Container

Supported by default:

Tomcat (default)

< Dependency > 
   < the groupId > org.springframework.boot </ the groupId > 
   < the artifactId > Spring-Boot-Starter-web </ the artifactId > 
   incorporated default web module is used as an embedded Tomcat Servlet Container; 
</ 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>

 

 

 

Guess you like

Origin www.cnblogs.com/xiaoqiqistudy/p/11366461.html