springboot整合listener方式一

  1. 创建一个项目
  2. package com.test;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.web.servlet.ServletComponentScan;
    
    @SpringBootApplication
    @ServletComponentScan//会扫描@WebServlet的类并实例化该类
    public class App {
    	public static void main(String[] args) {
    		SpringApplication.run(App.class, args);
    	}
    }
    

     

  3. 创建一个listener类

  4. package com.test.listener;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    import org.apache.catalina.SessionEvent;
    import org.apache.catalina.SessionListener;
    /***
     * 
     * 		com.test.listener.FirstListener
     * 
     * @author 26920
     *
     */
    @WebListener
    public class FirstListener implements ServletContextListener{
    	@Override
    	public void contextInitialized(ServletContextEvent sce) {
    		// TODO Auto-generated method stub
    		System.out.println("ServletContext初始化完成==========");
    		
    	}
    	@Override
    	public void contextDestroyed(ServletContextEvent sce) {
    		// TODO Auto-generated method stub
    		System.out.println("ServletContxt销毁了============");
    	}
    }
    

     

  5. 需要哪个监听器就实现哪个接口

你可能感兴趣的:(spring,boot)