zoukankan      html  css  js  c++  java
  • Spring Boot 整合 Listener

    两种方法:

     方法一:

      使用注解

    编写Listener,并使用@WebListener注解标记,在启动类使用注解:@ServletComponentScan

    package clc.user.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    /**
     * ClassName: FirstListener<br/>
     * Description: <br/>
     * date: 2019/4/2 11:04 AM<br/>
     *
     * @author chengluchao
     * @since JDK 1.8
     */
    
    @WebListener
    public class FirstListener implements ServletContextListener {
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            System.out.println("FirstListener-contextInitialized");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            System.out.println("FirstListener-contextDestroyed");
        }
    }

    启动类

    @SpringBootApplication
    @ServletComponentScan
    public class UserServiceApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(UserServiceApplication.class, args);
        }
    }

    方法二:

      在启动类使用java代码配置

    package clc.user.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    /**
     * ClassName: SecondListener<br/>
     * Description: <br/>
     * date: 2019/4/2 11:04 AM<br/>
     *
     * @author chengluchao
     * @since JDK 1.8
     */
    
    public class SecondListener implements ServletContextListener {
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            System.out.println("SecondListener-contextInitialized");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            System.out.println("SecondListener-contextDestroyed");
        }
    }

    启动类:

      

    @SpringBootApplication
    public class UserServiceApplication2 {
    
        @Bean
        public ServletListenerRegistrationBean getServletListenerRegistrationBean() {
            ServletListenerRegistrationBean listener = new ServletListenerRegistrationBean(new SecondListener());
            return listener;
        }
    
        public static void main(String[] args) {
            SpringApplication.run(UserServiceApplication2.class, args);
        }
    }
  • 相关阅读:
    论文解析 -- TiDB: A Raftbased HTAP Database
    人生资产负债表
    Hadoop、Hive、Spark 之间关系
    在 Python3 中,bytes 和 str 的互相转换方式是
    json中load和loads区别
    springboot——修改html实时生效,不用重启tomca(idea版)
    ThinkPHP6 利用crontab+think make:command执行定时任务 tp6默认不可以用命令行访问控制器
    Whoops, GitLab is taking too much time to respond.解决
    phpmyadmin导入csv文件 #1366
    pipenv --python 'python/path' install 报错原因
  • 原文地址:https://www.cnblogs.com/chenglc/p/10641491.html
Copyright © 2011-2022 走看看