• SpringBoot之Web原生组件注入



    前言

    注入Web原生Servlet、Filter、Listeber以及切换Web服务器。


    一、原生注解方式注入

    官方文档 - Servlets, Filters, and listeners

    Servlet注入:

    @WebServlet(urlPatterns = "/my")
    public class MyServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("66666");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Filter注入:

    @Slf4j
    @WebFilter(urlPatterns={"/css/*","/images/*"})
    public class MyFilter implements Filter {
        @Override
        public void init(FilterConfig filterConfig) throws ServletException {
            log.info("MyFilter初始化完成");
        }
    
        @Override
        public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
            log.info("MyFilter工作");
            chain.doFilter(request,response);
        }
    
        @Override
        public void destroy() {
            log.info("MyFilter销毁");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Listener注入:

    @Slf4j
    @WebListener
    public class MyServletContextListener implements ServletContextListener {
    
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            log.info("MySwervletContextListener监听到项目初始化完成");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            log.info("MySwervletContextListener监听到项目销毁");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    最后还要在主启动类添加注解@ServletComponentScan:

    @SpringBootApplication
    @ServletComponentScan(basePackages = "com.dragon.admin")//开启servlet注入组件扫描
    public class Springboot4Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Springboot4Application.class, args);
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    二、Spring方式注入

    首先将上面的三个类上的注释都取消,作普通类。

    ServletRegistrationBean, FilterRegistrationBean, ServletListenerRegistrationBean

    @Configuration(proxyBeanMethods = true)
    public class MyRegisterConfig {
        @Bean
        public ServletRegistrationBean myServlet(){
            Myservlet myservlet = new Myservlet();
            return new ServletRegistrationBean(myservlet,"/my","/my02");
        }
    
        @Bean
        public FilterRegistrationBean myFilter(){
            MyFilter myFilter = new MyFilter();
    //        return new FilterRegistrationBean(myFilter,myServlet());
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(myFilter);
            filterRegistrationBean.setUrlPatterns(Arrays.asList("/my","/css/*"));
            return filterRegistrationBean;
        }
    
        @Bean
        public ServletListenerRegistrationBean myListener(){
            MyServletContextListener myServletContextListener = new MyServletContextListener();
            return new ServletListenerRegistrationBean(myServletContextListener);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    三、切换web服务器与定制化

    • 默认支持的WebServer

      • Tomcat, Jetty, or Undertow
      • ServletWebServerApplicationContext 容器启动寻找ServletWebServerFactory 并引导创建服务器。
    • 原理

      • SpringBoot应用启动发现当前是Web应用,web场景包-导入tomcat。
      • web应用会创建一个web版的IOC容器 ServletWebServerApplicationContext
      • ServletWebServerApplicationContext 启动的时候寻找 ServletWebServerFactory (Servlet 的web服务器工厂——>Servlet 的web服务器)。
      • SpringBoot底层默认有很多的WebServer工厂(ServletWebServerFactoryConfiguration内创建Bean),如:
        • TomcatServletWebServerFactory
        • JettyServletWebServerFactory
        • UndertowServletWebServerFactory
      • 底层直接会有一个自动配置类ServletWebServerFactoryAutoConfiguration
      • ServletWebServerFactoryAutoConfiguration导入了ServletWebServerFactoryConfiguration(配置类)。
      • ServletWebServerFactoryConfiguration 根据动态判断系统中到底导入了那个Web服务器的包。(默认是web-starter导入tomcat包),容器中就有 TomcatServletWebServerFactory
      • TomcatServletWebServerFactory 创建出Tomcat服务器并启动;TomcatWebServer 的构造器拥有初始化方法initialize——this.tomcat.start();
      • 内嵌服务器,与以前手动把启动服务器相比,改成现在使用代码启动(tomcat核心jar包存在)。

    Spring Boot默认使用Tomcat服务器,若需更改其他服务器,则修改工程pom.xml:
    这是修改成jetty的服务器

    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-webartifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-tomcatartifactId>
            exclusion>
        exclusions>
    dependency>
    
    <dependency>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-jettyartifactId>
    dependency>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    官方文档 - Use Another Web Server


    总结

    以上就是Web原生组件注入。

  • 相关阅读:
    排序算法:选择排序
    大龄程序员如何面对“中年危机”?这份书单或许能帮到你
    轻松读懂spring之 IOC的主干流程
    JavaIO流:概述
    C++中的多线程编程:高效的并发处理方式
    C#毕业设计——基于C#+asp.net+sqlserver的汽车修理厂物资流通管理系统设计与实现(毕业论文+程序源码)——物资流通管理系统
    【Python小项目之Tkinter应用】随机点名/抽奖工具大优化:实现背景图与其他组件自适应窗口大小变化并保持相对位置和比例不变
    第三方登录功能的实现之 QQ登录 - 已绑定账号
    C语言 100道经典编程题适用于专升本,专接本【详细分析版】
    【学习QT必备的C++基础】C++类和对象
  • 原文地址:https://blog.csdn.net/weixin_62951900/article/details/133344925