zoukankan      html  css  js  c++  java
  • 02-03:springboot 整合listener

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

    1.1 编写listener

    /**
     * Springboot 整合listener
     */
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    @WebListener
    public class FirstListener implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            // TODO Auto-generated method stub
            System.out.println("listener Init");
        }
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            // TODO Auto-generated method stub
        }
    }

    1.2编写启动类

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

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

    2.1编写listener

    public class SecondListener implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            // TODO Auto-generated method stub
            System.out.println("secondListener....");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            // TODO Auto-generated method stub
    
        }
    
    }

    2.2 编写启动器

    /**
     * SpringBoot 整合Listener方式2
     * 
     * @author Administrator
     *
     */
    @SpringBootApplication
    public class App2 {
        public static void main(String[] args) {
            SpringApplication.run(App2.class, args);
        }
        /**
         * 注册listener
         */
        @Bean
        public ServletListenerRegistrationBean<SecondListener> getServletListenerRegistrationBean(){
            ServletListenerRegistrationBean<SecondListener> bean= new ServletListenerRegistrationBean<SecondListener>(new SecondListener());
            return bean;
        }
    }
  • 相关阅读:
    java基础(二):补充
    java基础(二):变量和数据类型
    C基础系列(一)
    java基础(一)
    循环小数(Repeating Decimals)
    DNA序列(DNA Consensus String)
    sqlserver~创建函数
    docker简介和安装
    JMeter(6)、实现Java请求
    JMeter(5)、测试数据库
  • 原文地址:https://www.cnblogs.com/wangjianly/p/9794324.html
Copyright © 2011-2022 走看看