• Springboot 配置嵌入式 Servlet 容器


    一、如何定制和修改 Servlet 容器的相关配置

    1、通过修改 application.properties 配置文件,例如

    //通用的Servlet容器设置
    server.xxx
    // 端口设置
    server.port=8081
    // 上下文根设置
    server.context-path=/xiaomaomao
    // tomcat 的设置
    server.tomcat.xxx
    // jetty 的设置
    server.jetty.xxx

    所有 Servlet 容器相关的配置都与 ServerProperties 进行了相关的绑定

     

    假设我们这里嵌入式的 Servlet 容器是 tomcat,我们需要修改 tomcat 相关配置,只需要在 application.properties 中进行配置就可以了,具体可以配置的选项如下

    2、通过往容器中注入 bean 的方式, Springboot 1.x 版本 和 Springboot 2.x 版本注入的 bean 不同

    Springboot 1.x 版本:编写一个嵌入式 Servlet 容器定制器 EmbeddedServletContainerCustomizer , 通过调用其中的 Custome

    @Configuration
    public class MyEmbeddedContainerConfig {
        @Bean
        public EmbeddedServletContainerCustomizer myCustomizer(){
            EmbeddedServletContainerCustomizer servletContainerCustomizer = new EmbeddedServletContainerCustomizer() {
                @Override
                public void customize(ConfigurableEmbeddedServletContainer container) {
                    container.setContextPath("/xiaomaomao");
                    container.setPort(8084);
                }
            };
            return servletContainerCustomizer;
        }
    }
    

    Springboot 2.x 版本:编写一个 WebServerFactoryCustomizer ,并将其加入容器中

    @Configuration
    public class MyEmbeddedContainerConfig {
        @Bean
        public WebServerFactoryCustomizer myCustomizer(){
            return new WebServerFactoryCustomizer<TomcatServletWebServerFactory>() {
                @Override
                public void customize(TomcatServletWebServerFactory factory) {
                    factory.setPort(8084);
                    factory.setContextPath("/xiaomaomao");
                }
            };
        }
    }
    

    二、注册 Servlet 三大组件 (Servlet、Filter、Listener)

    1、注册 Servlet

    @Bean
    public ServletRegistrationBean servletBean(){
    	// 自定义 MyEmbeddedServlet 实现 HttpServlet
    	ServletRegistrationBean<MyEmbeddedServlet> myEmbedServlet = new
    			ServletRegistrationBean<>(new MyEmbeddedServlet(), "/myEmbedServlet");
    	return myEmbedServlet;
    }
    

    2、注册 Filter

    @Bean
    public FilterRegistrationBean filterBean(){
    	FilterRegistrationBean<MyEmbeddedFilter> filterRegistrationBean = new FilterRegistrationBean<>();
    	filterRegistrationBean.setFilter(new MyEmbeddedFilter());
    	filterRegistrationBean.setUrlPatterns(Arrays.asList("/xiaomaomao","/myEmbedServlet"));
    	return filterRegistrationBean;
    }
    

    3、注册 Listener

    @Bean
    public ServletListenerRegistrationBean servletListenerRegistrationBean(){
    	ServletListenerRegistrationBean listenerRegistrationBean =
    			new ServletListenerRegistrationBean(new MyEmbeddedListener());
    	return listenerRegistrationBean;
    }

    Springboot 默认的嵌入式 Servlet 容器是 tomcat

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

     

    三、那么我们想切换其它的 Servlet 容器 (jetty、undertow),应该执行的步骤如下

    1、排除 Springboot 默认的嵌入式 Servlet 容器 tomcat 的依赖

    2、引入 jetty 或 undertow 的相关依赖即可

     

     

      

     

  • 相关阅读:
    pyenv: python2.7: command not found The `python2.7' command exists in these Python versions: 2.7.5
    Gazebo_02_build_a_robot
    Gazebo_01_getting_start
    vscode等编辑器中报Exception has occurred: ModuleNotFoundError No module named 'requests'
    Ubuntu16.04安装Python3.8以后出现lsb_release/No LSB modules are available的错误
    C语言字符串定义(数组&指针)
    电脑软件更新管理
    VS2017自定义新建模板
    《SQL必知必会-第四版》--学习摘抄
    实体类封装数据库查询信息(工具一)
  • 原文地址:https://www.cnblogs.com/xiaomaomao/p/14033112.html
Copyright © 2020-2023  润新知