SpringBoot中Web原生组件注入(Servlet、Filter、Listener)

SpringBoot中Web原生组件注入(Servlet、Filter、Listener)


文章目录

  • SpringBoot中Web原生组件注入(Servlet、Filter、Listener)
    • 一、使用Servlet API
    • 二、使用RegistrationBean
      • 2.1 ServletRegistrationBean

一、使用Servlet API

  1. 在主类中添加@ServletComponentScan(basePackages = "cn.edu.tiangong")注解。

    1. 作用:制定原生servlet组件都放在哪里

    2. 代码:

      @ServletComponentScan(basePackages = "cn.edu.tiangong")
      @SpringBootApplication
      public class SpringBootDemo05WebApplication {
      
          public static void main(String[] args) {
              SpringApplication.run(SpringBootDemo05WebApplication.class, args);
          }
      
      }
      
  2. 编写一个servlet实现HttpServlet,并添加注解@WebServlet(urlPatterns = "/my")

    1. 效果:直接响应,没有经过SpringBoot的拦截器

    2. 代码:

      @WebServlet(urlPatterns = "/my")
      public class MyServlet extends HttpServlet {
      
          @Override
          protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
              resp.getWriter().write("6666");
          }
      }
      
  3. 编写一个filter实现Filter接口,并且添加@WebFilter(urlPatterns = {"/css/*","/image/*"})注解

    1. 效果:对根目录下css和image路径下的所有访问请求进行过滤操作

    2. 代码:

      @Log4j2
      @WebFilter(urlPatterns = {"/css/*","/image/*"})
      // /*是servlet的写法
      // /**是Spring的写法
      public class MyFilter implements Filter {
      
          @Override
          public void init(FilterConfig filterConfig) throws ServletException {
              log.info("MyFilter初始化了");
          }
      
          @Override
          public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
              log.info("MyFilter工作");
              filterChain.doFilter(servletRequest, servletResponse);
          }
      
          @Override
          public void destroy() {
              log.info("MyFilter销毁了");
          }
      }
      

二、使用RegistrationBean

  • ServletRegistrationBean
  • FilterRegistrationBean
  • ServletListenerRegistrationBean

2.1 ServletRegistrationBean

  1. 先写一个Servlet,这个servlet不用添加@WebServlet注解

    public class MyServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.getWriter().write("6666");
        }
    }
    
  2. 再写一个MyRegistrationConfig,添加@Configuration注解

    /**
     * 【注意事项】
     * 1. 没有添加(proxyBeanMethods = false),这样可以保证依赖的组件始终是单实例的,防止容器中组件冗余
     * 2. 为什么自己写的myServlet是直接响应的没有经过SpringBoot的拦截器???
     */
    //@Configuration(proxyBeanMethods = false)
    @Configuration(proxyBeanMethods = true)
    public class MyRegistrationConfig {
    
        @Bean
        public ServletRegistrationBean myServlet() {
            MyServlet servlet = new MyServlet();
            return new ServletRegistrationBean(servlet, "/my", "/your");
        }
    
        @Bean
        public FilterRegistrationBean myFilter() {
            MyFilter filter = new MyFilter();
            // 1. 只拦截myServlet的路径
    //        return new FilterRegistrationBean(filter, myServlet());
            // 2. 拦截自定义路径
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(filter);
            filterRegistrationBean.setUrlPatterns(Arrays.asList("/my", "/css/*"));
            return filterRegistrationBean;
        }
    }
    

你可能感兴趣的:(SpringBoot,spring,boot,java,spring,后端)