1、通过注解扫描完成 Listener组件的注册
/**
* springBoot 整合 Listener
*
*<listener>
* <listener-class>com.listener.MyListener</listener-class>
*</listener>
*/
@WebListener
public class MyListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void contextInitialized(ServletContextEvent arg0) {
System.out.println("MyListener ...init......");
}
}
/**
* springBoot 整合 Listener 方式一
*/
@SpringBootApplication
@ServletComponentScan
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
/**
* SpringBoot 整合 Listener 方式二
*/
@SpringBootApplication
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
/**
* 注册 listener
*/
@Bean
public ServletListenerRegistrationBean<MyListener>
getServletListenerRegistrationBean(){
ServletListenerRegistrationBean<MyListener> bean= new
ServletListenerRegistrationBean<SecondListener>(new MyListener());
return bean;
}
}