关于Spring JavaWeb工程中的ContextRefreshedEvent事件

在应用启动时,通常想在此时预加载一些资源,全局使用。

Spring会在操作应用上下文时,使用ApplicationEventPublisher触发相关ApplicationContextEvent,我们可以监听这些事件来做一些事情。

Spring中ApplicationContextEvent有以下几种:

其中ContextRefreshedEvent的执行时机为:

1 Event raised when an {@code ApplicationContext} gets initialized or refreshed.

我们通常会在Spring加载或刷新应用上下文时,也重新刷新下我们预加载的资源,我们就可以通过监听ContextRefreshedEvent来做这样的事情。

代码如下:

1 @Component
2 public class SpringHandlersProvider implements ApplicationListener<ContextRefreshedEvent> {
3     Lists<XXX> handlerList = Lists.newHashList();
4     @Override
5     public void onApplicationEvent(ContextRefreshedEvent event) {  
6           //do something
7          handlerList.add(xxx);
8     }
9 }

但对于tomcat工程来说,我们一般会加载两个上下文容器一个父容器,一个mvc子容器

  1. 父容器{@code ContextRefreshedEvent[source=Root WebApplicationContext: startup date [Thu Sep 29 14:52:08 CST 2016]; root of context hierarchy]}
  2. mvc容器{@code ContextRefreshedEvent[source=WebApplicationContext for namespace 'springmvc-servlet': startup date [Thu Sep 29 14:52:34 CST 2016]; parent: Root WebApplicationContext]}

这样就会触发两次ContextRefreshedEvent事件,导致监听此事件所作的逻辑执行两次。

避免方法:

1:只在加载父容器时,执行一次

 1 @Component
 2 public class SpringHandlersProvider implements ApplicationListener<ContextRefreshedEvent> {
 3     Lists<XXX> handlerList = Lists.newHashList();
 4     @Override
 5     public void onApplicationEvent(ContextRefreshedEvent event) { 
 6         if (Predicates.isNull().apply(event.getApplicationContext().getParent())) {
 7               //do something
 8              handlerList.add(xxx);
 9         }
10     }
11 }    

2:每次执行onApplicationEvent()方法时就将存放资源的容器清空下

 1 @Component
 2 public class SpringHandlersProvider implements ApplicationListener<ContextRefreshedEvent> {
 3     Lists<XXX> handlerList = Lists.newHashList();
 4     @Override
 5     public void onApplicationEvent(ContextRefreshedEvent event) {  
 6          handlerList.clear();
 7 
 8           //do something
 9          handlerList.add(xxx);
10     }
11 }
原文地址:https://www.cnblogs.com/halu126/p/ContextRefreshedEvent.html