• SpringBoot2.0---------------14、SpringBoot中Web原生组件注入


    1、使用Servlet API (Servlet、Filter过滤器、Listener监听器)
    ● @ServletComponentScan(basePackages = “com.atguigu.admin”) :指定原生Servlet组件都放在那里,改注解放在SpringBoot主程序中

    @MapperScan("com.atguigu.admin.mapper")
    @ServletComponentScan(basePackages = "com.atguigu.admin")
    @SpringBootApplication(exclude = RedisAutoConfiguration.class)
    public class Boot05WebAdminApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(Boot05WebAdminApplication.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    ● @WebServlet(urlPatterns = “/my”):效果:直接响应,没有经过Spring的拦截器

    @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
    • 9

    ● @WebFilter(urlPatterns={“/css/“,”/images/”}) 原生态过滤器

    @Slf4j
    @WebFilter(urlPatterns={"/css/*","/images/*"}) //my
    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

    ● @WebListener 原生态监听

    @Slf4j
    @WebListener
    public class MySwervletContextListener 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

    推荐可以这种方式;

    扩展:DispatchServlet 如何注册进来
    ● 容器中自动配置了 DispatcherServlet 属性绑定到 WebMvcProperties;对应的配置文件配置项是 spring.mvc。
    ● 通过 ServletRegistrationBean 把 DispatcherServlet 配置进来。
    ● 默认映射的是 / 路径。

    在这里插入图片描述
    Tomcat-Servlet;
    多个Servlet都能处理到同一层路径,精确优选原则
    A: /my/
    B: /my/1

    2、使用RegistrationBean

    ServletRegistrationBean, FilterRegistrationBean, and ServletListenerRegistrationBean

    // (proxyBeanMethods = true):保证依赖的组件始终是单实例的
    @Configuration(proxyBeanMethods = true)
    public class MyRegistConfig {
    
        @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(){
            MySwervletContextListener mySwervletContextListener = new MySwervletContextListener();
            return new ServletListenerRegistrationBean(mySwervletContextListener);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

  • 相关阅读:
    Windows与网络基础-18-注册表基础
    Matplotlib可视化50图:气泡图(2)
    独立按键程序
    C++学习day3
    SpringBoot 日志
    R语言使用oneway.test函数执行单因素方差分析(One-Way ANOVA)、使用数据集的子集数据进行单因素方差分析(subset函数筛选数据子集)
    南京--ChatGPT/GPT4 科研实践应用
    JAVA毕设项目软考网络工程师在线练习平台(java+VUE+Mybatis+Maven+Mysql)
    台灯怎么选对眼睛好?精选眼科医生推荐护眼灯
    Educational Codeforces Round 137 (Rated for Div. 2)练习笔记
  • 原文地址:https://blog.csdn.net/lyy_sss/article/details/126284843