监听spring 容器启动事件 ContextRefreshEvent 与 ApplicationStartedEvent区别
@Slf4j@Componentpublic class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> {@Resourceprivate EsFindService esFindService;@Overridepublic ...
@Slf4j
@Component
public class ContextRefreshListener implements ApplicationListener<ContextRefreshedEvent> {
@Resource
private EsFindService esFindService;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
log.info("ContextRefreshedEvent hahah esFindService:{} ",esFindService);
}
}
@Component
@Slf4j
public class MqClueConsumeListener implements ApplicationListener<ApplicationStartedEvent>{
@Override
public void onApplicationEvent(ApplicationStartedEvent arg0) {
log.info("hahahahahhaha ApplicationStartedEvent");
}
}
2020-02-07 20:14:24.553 INFO [crm,,,] 17783 --- [ main] c.h.crm.listener.ContextRefreshListener : ContextRefreshedEvent hahah esFindService:null
2020-02-07 20:14:27.740 INFO [crm,,,] 17783 --- [ main] c.h.crm.listener.ContextRefreshListener : ContextRefreshedEvent hahah esFindService:com.hzrys.crm.service.impl.EsFindServiceImpl@25a2c4dc
2020-02-07 20:34:56.156 [,] [main] INFO c.h.c.l.MqClueConsumeListener - [onApplicationEvent,72] - hahahahahhaha ApplicationStartedEvent
可以看出ContextRefreshEvent 事件触发2次。 并且第一次,spring factroyBean容器还没初始化完毕,esFindService =null;
而ApplicationStartedEvent只触发一次,并且容器已初始化完毕了。
更多推荐
所有评论(0)