zoukankan      html  css  js  c++  java
  • SpringBoot整合Lintener

    1.通过扫描完成Lintener组件的注册

    1.1编写Listener

    /**
     * springboot整合Lintener 方式一
     * 在web.xml中如何配置Listener
     * <listener>
     *     <listener-class>com.demo.istener.FirstListener</listener-class>//实例化listener全名
     * </listener>
     *
     * servlet上下文的监听器
     */
    @WebListener
    public class FirstListener implements ServletContextListener {
        /**
         * 当servlet容器终止web
         *
         * @param sce
         */
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
    
        }
    
        /**
         * 当servletr容器启动web
         * @param sce
         */
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            System.out.println("Listener....init...");
        }
    }

    1.2编写启动类

    /**
     * springboot整合Lintener 方式一
     */
    @SpringBootApplication
    @ServletComponentScan
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class,args);
        }
    }

    1.3启动main方法

    2.通过方法完成Lintener组件的注册

    2.1 编写Listener

    /**
     * springboot整合Lintener 方式二
     */
    public class SecondListener implements ServletContextListener {
    
        /**
         * servletr容器启动web
         * @param sce
         */
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            System.out.println("SecondListener....init...");
        }
    
        /**
         * servlet容器终止web
         * @param sce
         */
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
    
        }
    }

    2.2编写启动类

    /**
     * springboot整合Lintener 方式二
     */
    @SpringBootApplication
    public class App2 {
        public static void main(String[] args) {
            SpringApplication.run(App2.class,args);
        }
    
        /**
         * 注册Listener
         * @return
         */
        @Bean
        public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
            ServletListenerRegistrationBean<SecondListener> bean=new ServletListenerRegistrationBean<>(new SecondListener());
            return bean;
        }
    }
    

     2.3启动,结果

    小结 整合Listener两种方式:1.@WebListener  @SpringBootApplication @ServletContenerScan   2.@SpringBootApplication @Bean ServletListenerRegistrationBean<SecondListener>

  • 相关阅读:
    【小程序】订阅消息
    【小程序】轮播图
    【小程序】全局变量的设置、使用、修改、全局方法执行
    【RN】标题栏右边添加自定义按钮或加事件
    【RN】阴影react-native-shadow
    【vue】点击复制到剪贴板的方法( clipboard )
    Q-learning和Sarsa的区别
    Q-learning之一维世界的简单寻宝
    使用tensorflow时,关于GPU的设置
    安装Matlab出现弹出DVD1插入DVD2的提示怎么办?
  • 原文地址:https://www.cnblogs.com/wangshuang123/p/10849425.html
Copyright © 2011-2022 走看看