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();
   }
  }
 }
}
 
}

  

posted @ 2018-12-05 16:44  尐鱼儿  阅读(233)  评论(0编辑  收藏  举报