简介
ApplicationListener是spring提供的接口,作用是在启动时去加载某些程序。
内置事件
1、ContextRefreshedEvent:所有Bean均已被成功加载后的事件。
2、ContextStartedEvent:当使用ApplicationContext的子接口(ConfigurableApplicationCntext)接口中的start方法时,触发的事件。
3、ContextStoppedEvent:当使用ApplicationContext的子接口(ConfigurableApplicationCntext)接口中的stop方法时,触发的事件。
4、ContextClosedEvent:关闭程序触发的事件。
执行顺序
@Service
@Slf4j
public class TestService {
@PostConstruct
private void fixedTimeInvoke() {
log.info("=====PostConstruct=====");
}
}
@Component
@Slf4j
public class MyApplicationListener1 implements ApplicationListener<ContextRefreshedEvent> {
@Override
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
log.info("=====ContextRefreshedEvent=====");
log.info("getApplicationContext: "+contextRefreshedEvent.getApplicationContext());
log.info("class: "+contextRefreshedEvent.getClass());
log.info("timeStamp: "+contextRefreshedEvent.getTimestamp());
log.info("source: "+contextRefreshedEvent.getSource());
log.info("=================================================");
}
}
@Component
@Slf4j
public class MyApplicationListener2 implements ApplicationListener<ApplicationEvent> {
@Override
public void onApplicationEvent(ApplicationEvent applicationEvent) {
log.info("=====ApplicationEvent=====");
log.info("class: "+applicationEvent.getClass());
log.info("timeStamp: "+applicationEvent.getTimestamp());
log.info("source: "+applicationEvent.getSource());
log.info("=================================================");
}
}
@Component
@Slf4j
public class MyApplicationListener3 implements ApplicationListener<ContextStartedEvent> {
@Override
public void onApplicationEvent(ContextStartedEvent contextStartedEvent) {
log.info("=====ContextStartedEvent=====");
log.info("getApplicationContext: "+contextStartedEvent.getApplicationContext());
log.info("class: "+contextStartedEvent.getClass());
log.info("timeStamp: "+contextStartedEvent.getTimestamp());
log.info("source: "+contextStartedEvent.getSource());
log.info("=================================================");
}
}
@Component
@Slf4j
public class MyApplicationListener4 implements ApplicationListener<ContextStoppedEvent> {
@Override
public void onApplicationEvent(ContextStoppedEvent contextStoppedEvent) {
log.info("=====ContextStoppedEvent=====");
log.info("getApplicationContext: "+contextStoppedEvent.getApplicationContext());
log.info("class: "+contextStoppedEvent.getClass());
log.info("timeStamp: "+contextStoppedEvent.getTimestamp());
log.info("source: "+contextStoppedEvent.getSource());
log.info("=================================================");
}
}
@Component
@Slf4j
public class MyApplicationListener5 implements ApplicationListener<ContextClosedEvent> {
@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
log.info("=====ContextClosedEvent=====");
log.info("getApplicationContext: "+contextClosedEvent.getApplicationContext());
log.info("class: "+contextClosedEvent.getClass());
log.info("timeStamp: "+contextClosedEvent.getTimestamp());
log.info("source: "+contextClosedEvent.getSource());
log.info("=================================================");
}
}
结果
=====PostConstruct=====
=====ContextRefreshedEvent=====
getApplicationContext: com.alipay.jarslink.api.impl.ModuleAnnotationApplicationContext@3197fc1e, started on Fri Sep 30 16:02:34 CST 2022, parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@48d7ad8b
class: class org.springframework.context.event.ContextRefreshedEvent
timeStamp: 1664524955460
source: com.alipay.jarslink.api.impl.ModuleAnnotationApplicationContext@3197fc1e, started on Fri Sep 30 16:02:34 CST 2022, parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@48d7ad8b
=================================================
=====ApplicationEvent=====
class: class org.springframework.context.event.ContextRefreshedEvent
timeStamp: 1664524955460
source: com.alipay.jarslink.api.impl.ModuleAnnotationApplicationContext@3197fc1e, started on Fri Sep 30 16:02:34 CST 2022, parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@48d7ad8b
=================================================
=====ApplicationEvent=====
class: class org.springframework.boot.builder.ParentContextApplicationContextInitializer$ParentContextAvailableEvent
timeStamp: 1664524955462
source: com.alipay.jarslink.api.impl.ModuleAnnotationApplicationContext@3197fc1e, started on Fri Sep 30 16:02:34 CST 2022, parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@48d7ad8b
=================================================
=====ApplicationEvent=====
class: class org.springframework.context.event.ContextClosedEvent
timeStamp: 1664524964321
source: com.alipay.jarslink.api.impl.ModuleAnnotationApplicationContext@3197fc1e, started on Fri Sep 30 16:02:34 CST 2022, parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@48d7ad8b
=================================================
=====ContextClosedEvent=====
getApplicationContext: com.alipay.jarslink.api.impl.ModuleAnnotationApplicationContext@3197fc1e, started on Fri Sep 30 16:02:34 CST 2022, parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@48d7ad8b
class: class org.springframework.context.event.ContextClosedEvent
timeStamp: 1664524964321
source: com.alipay.jarslink.api.impl.ModuleAnnotationApplicationContext@3197fc1e, started on Fri Sep 30 16:02:34 CST 2022, parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@48d7ad8b
=================================================