import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
importorg.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component
public class InitDataListener implements
ApplicationListener { //ContextRefreshedEvent为初始化完毕事件
private static final Logger LOGGER =Logger
.getLogger(InitDataListener.class);
// 缓存管理器
@Autowired
@Qualifier("dictCacheManager")
private DictCacheManager dictCacheManager;
@Override
public voidonApplicationEvent(ContextRefreshedEvent event) {
final ApplicationContext app= event.getApplicationContext();
if (null ==app.getParent()
&&"Root WebApplicationContext".equals(app.getDisplayName())) { //当存在父子容器时,此判断很有用
//读取字典配置
Map config =new HashMap();
try {
config= PathUtil.getProperties("dataDictConfig.properties");
finalString mKey = config.get("dataDictKey");
if(StringUtils.isNotBlank(mKey)) {
DictCacheManager.M_KEY= mKey;
}
} catch(Exception e) {
LOGGER.error("Load\"dataDictConfig.properties\" failure!", e);
}
//判断是否立即加载数据字典到Redis
if("false".equals(config.get("lazyLoad"))) { //立即加载数据字典数据到Redis缓存服务器
dictCacheManager.init();
}
}
}
}
}
Spring监听,ApplicationListener
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
理解 Spring ApplicationListener
ApplicationContext事件机制是观察者设计模式的实现,通过ApplicationEvent类和ApplicationListener
ApplicationListener spring 初始化 自定义 -
【ApplicationListener】Springboot各类事件监听器
Springboot中SpringApplicationEvent的种类 如下图: 主要包括6种: 1. ApplicationEnvironmentPreparedListener 2. ApplicationFailedListener 3. ApplicationPreparedListene
springboot ApplicationListener 监听器 spring tomcat -
【Spring Boot 源码学习】ApplicationListener 详解
本篇 Huazie 带大家详细分析了 ApplicationListener 的加载和处理应用程序事件,这对于后续的 SpringApplication 运行流程的理解至关重要。
Spring Boot 源码学习 ApplicationListener -
spring事件ApplicationEvent/ApplicationListener/@EventListener注解
spring自带事件功her,...
spring事件 @EventListener ApplicationEvent spring -
SpringBoot中ApplicationListener事件监听的6种方式
Event事件
spring boot event spring 监听器 事件源 -
ApplicationListener事件监听、自定义事件、异步执行
ApplicationEvent以及Listener是Spring为我们提供的一个事件监听、订阅的实现,内部实现原理是观察者设计模式,设计初衷也是为了系统业务逻辑之extClosedEvent如何使
spring 观察者模式 发布订阅 EnableAsync Async