Spring监听,ApplicationListener

春衣少年当酒歌,起舞四顾以笑和。这篇文章主要讲述Spring监听,ApplicationListener相关的知识,希望能为你提供帮助。

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】

    推荐阅读