• springBoot整合Listener


    新建项目

     

     

     这个是pom文件

    <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>

     新建FirstListener类

     

    FirstListener.java
    package com.gongspringlister.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    /**
     * springBoot 整合 Listener
     * *
     <listener>
     * <listener-class>com.bjsxt.listener.FirstListener</listener-class>
     *</listener>
     */
    @WebListener
    public class FirstListener implements ServletContextListener {
        @Override
        public void contextDestroyed(ServletContextEvent arg0) {
    
        }
    
        @Override
        public void contextInitialized(ServletContextEvent arg0) {
            System.out.println("Listener...init......");
        }
    }

     新建App.java启动类

     

    App.java

    package com.gongspringlister;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    /**
     * springBoot 整合 Listener 方式一
     ** *
     */
    @SpringBootApplication
    @ServletComponentScan
    public class App {
        public static void main(String[] args){
            SpringApplication.run(App.class,args);
        }
    }

    运行启动类

     

     SpringBoot 整合 Listener 方式二

     新建SecondListener类

     

    SecondListener.java

    package com.gongspringlister.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    /**
     * springBoot 整合 Listener 方式二
     * * *
     */
    @WebListener
    public class SecondListener implements ServletContextListener {
        @Override
        public void contextDestroyed(ServletContextEvent arg0) {
    
        }
    
        @Override
        public void contextInitialized(ServletContextEvent arg0) {
            System.out.println("Listener...init......");
        }
    }

    编写启动类App2.java

    App2.java

    package com.gongspringlister;
    
    import com.gongspringlister.listener.SecondListener;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
    import org.springframework.context.annotation.Bean;
    
    /**
     * SpringBoot 整合 Listener 方式二* * *
     */
    @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;
        }
    }

    运行启动类App2.java

     

  • 相关阅读:
    jdk版本切换
    Java开发中遇到的问题
    递归删除文件夹
    重写equals方法
    JSP基础
    js把变量转换成json数据
    myBatista批量查询和插入
    Jquery密码强度校验
    Linux配置外网访问mysql
    linux下开启、关闭、重启mysql服务命令
  • 原文地址:https://www.cnblogs.com/braveym/p/11302358.html
Copyright © 2020-2023  润新知