SpringBoot - 使用Listener
2021-05-16 21:30
标签:[] tde code ret 后台 wol override inf list 后台打印: 后台打印: SpringBoot - 使用Listener 标签:[] tde code ret 后台 wol override inf list 原文地址:https://www.cnblogs.com/simple-ly/p/9748278.html1、在SpringBoot中使用Listener
1.1、使用注解注册Listener:
/**
* SpringBoot使用 Listener
*/
@WebListener
public class OneListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("OneListener init ...");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
@SpringBootApplication
//在 springBoot 启动时会扫描@WebListener并实例化
@ServletComponentScan
public class OneListenerApp {
public static void main(String[] args) {
SpringApplication.run(OneListenerApp.class, args);
}
}
1.2、另一种初始化Filter的方法:方法注册
/**
* SpringBoot使用 Listener
*/
public class TwoListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
System.out.println("TwoListener init ...");
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
}
}
@SpringBootApplication
public class TwoListenerApp {
public static void main(String[] args) {
SpringApplication.run(TwoListenerApp.class, args);
}
@Bean
public ServletListenerRegistrationBean registrationListenerBean(){
ServletListenerRegistrationBean bean = new ServletListenerRegistrationBean(new TwoListener());
return bean;
}
}
下一篇:【转】A*算法解决八数码问题
文章标题:SpringBoot - 使用Listener
文章链接:http://soscw.com/index.php/essay/86428.html