Spring Boot 配置和替换 嵌入式Servlet容器

Spring Boot 默认使用 Tomcat 作为嵌入式的 Servlet 容器:

Spring Boot 配置和替换 嵌入式Servlet容器_第1张图片

1,定制和修改Servlet容器相关的配置

     ①:修改和server有关的配置(ServerProperties【内部也是EmbeddedServletContainerCustomizer实现配置功能】):

#项目访问路径
server.port=8081
server.context-path=/crud

server.tomcat.uri-encoding=UTF-8

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

     ②:编写一个 EmbeddedServletContainerCustomizer:嵌入式的 Servlet 容器的定制器;来修改 Servlet 容器的配置

    //配置嵌入式的Servlet容器
    @Bean
    public EmbeddedServletContainerCustomizer embeddedServletContainerCustomizer(){
        return new EmbeddedServletContainerCustomizer() {

            //定制嵌入式的Servlet容器相关的规则
            @Override
            public void customize(ConfigurableEmbeddedServletContainer container) {
                container.setPort(8083);
            }
        };
    }

2,注册Servlet三大组件(Servlet,Filter,Listener)

     由于Spring Boot默认是以jar包的方式,启动嵌入式的Servlet容器来启动Spring Boot的Web应用,没有web.xml文件。

     注册三大组件用以下方式:

          ServletRegistraionBean;

public class MyServlet extends HttpServlet {

    //处理get请求
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("Hello MyServlet");
    }
}
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }

          FilterRegistraionBean;

public class MyFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        System.out.println("MyFilter process...");
        chain.doFilter(request,response);

    }

    @Override
    public void destroy() {

    }
}
    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return registrationBean;
    }

          ServletListenerRegistraionBean;

public class MyListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        System.out.println("contextInitialized...web应用启动");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("contextDestroyed...当前web项目销毁");
    }
}
    @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }

创建一个服务器配置类 MyServerConfig :

@Configuration
public class MyServerConfig {

    //注册Servlet容器
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        registrationBean.setLoadOnStartup(1);   //启动顺序
        return registrationBean;
    }
    //注册Filter过滤器
    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/hello","/myServlet"));
        return registrationBean;
    }
    //注册Listener监听器
    @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean 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);
            }
        };
    }

}

Spring Boot 自动配置 Spring MVC 的时候,自动注册 Spring MVC 的前端控制器: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;
}

3,Spring Boot 还支持的Servlet容器:

          Jetty(长连接(web聊天)) ;

          Undertow(不支持JSP,高性能,非阻塞,并发性能好);

可配置嵌入式Servlet容器:ConfigurableEmbeddedServletContainer

Spring Boot 配置和替换 嵌入式Servlet容器_第2张图片

Tomcat:;默认使用


   org.springframework.boot
   spring-boot-starter-web
   

Jetty:先排除默认的tomcat依赖,再引入Jetty容器的依赖
 



   org.springframework.boot
   spring-boot-starter-web
   
      
         spring-boot-starter-tomcat
         org.springframework.boot
      
   




   spring-boot-starter-jetty
   org.springframework.boot

Undertow:先排除默认的tomcat依赖,再引入Undertow容器的依赖


   org.springframework.boot
   spring-boot-starter-web
   
      
         spring-boot-starter-tomcat
         org.springframework.boot
      
   




   spring-boot-starter-undertow
   org.springframework.boot

 

你可能感兴趣的:(Spring-Boot)