springboot listener注册
*************************
相关注解
@WebListener
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebListener {
String value() default "";
}
*************************
示例
*******************
listener 层
MyListener:自定义listener
@WebListener("myListener")
public class MyListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("容器初始化");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("容器销毁");
}
}
*******************
config 层
WebConfig:扫描创建listener bean
@Configuration
@ServletComponentScan(basePackages = {"com.example.demo.listener"})
public class WebConfig implements WebMvcConfigurer{
}
*************************
容器启动控制台输出
2019-12-18 10:21:38.768 INFO 11896 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1119 ms
2019-12-18 10:21:38.822 INFO 11896 --- [ main] o.apache.catalina.core.StandardContext : Suspicious URL pattern: [/**] in context [], see sections 12.1 and 12.2 of the Servlet specification
容器初始化
2019-12-18 10:21:38.941 INFO 11896 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-12-18 10:21:39.093 INFO 11896 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-12-18 10:21:39.093 INFO 11896 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 1.916 seconds (JVM running for 3.442)