您的位置:首页 > 编程语言 > Java开发

spring启动时加载字典表数据放入map

2017-01-17 11:51 274 查看
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.zsmy.constant.Constant;
import cn.zsmy.service.tmp.ExDictService;
import cn.zsmy.tmp.extend.entity.ExDict;

/**
* 初始化系统参数
*
*/
@Service("dictInit")
public class DictInit {

/**存放系统参数*/
public static HashMap<String, String> dictMap = new HashMap<String, String>();

@Autowired
private ExDictService exDictService;

/**参数初始化工作*/
public void start() {
injectDictConfigByDb();
}

/**读取字典表 */
private void injectDictConfigByDb() {
Constant.MY_LOG.debug("-------Shm:injectDictConfigByDb--开始-------------------");
List<ExDict> exDictList = exDictService.findDict();
if(exDictList != null && exDictList.size() > 0){
for (ExDict exDict : exDictList) {
dictMap.put(exDict.getCode(), exDict.getdValue());
}
}
//打印map
HashMap<String, String> dictMap = (HashMap<String, String>) DictInit.dictMap;
for (HashMap.Entry<String, String> entry : dictMap.entrySet()) {
Constant.MY_LOG.debug("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Constant.MY_LOG.debug("-------Shm:injectDictConfigByDb--结束-------------------");
}
}


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import cn.zsmy.constant.Constant;

@Component("BeanDefineConfigue")
public class BeanDefineConfigue implements ApplicationListener<ContextRefreshedEvent> {

@Autowired
private DictInit dictInit;

//YC:当一个ApplicationContext被初始化或刷新触发
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
Constant.MY_LOG.debug("YYCC:START:spring初始化开始======================>");
if (event.getApplicationContext().getParent() == null) {//root application context 没有parent,他就是老大.
Constant.MY_LOG.debug("启动projectInit的start方法进行参数的初始化======================>");
dictInit.start();
} else {
//为什么会执行两次:请参考博文:http://www.cnblogs.com/yucongblog/p/5437744.html
Constant.MY_LOG.debug("spring初始化时,执行onApplicationEvent:event.getApplicationContext().getParent() != null======================>");
}
Constant.MY_LOG.debug("YYCC:END:spring初始化完毕======================>");
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: