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;
        }
    }
  • 相关阅读:
    (转)超过 130 个你需要了解的 vim 命令
    ubuntu下解压文件命令大全(转)
    HDU 4681 String
    Linux使用过程中常见问题及其解决方法
    Linux 命令 及 简单操作 学习
    HDU 4666 Hyperspace (最远曼哈顿距离)
    POJ 2049 Finding Nemo
    HDU 4655 Cut Pieces
    <textarea>标签的使用
    数据库插入失败 和回滚
  • 原文地址:https://www.cnblogs.com/wangjianly/p/9794324.html
Copyright © 2011-2022 走看看