zoukankan      html  css  js  c++  java
  • spring启动时加载字典表数据放入map

    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初始化完毕======================>");
        }
    
    }
  • 相关阅读:
    es6箭头函数 this 指向问题
    vue脚手架搭建移动端项目--flexible.js
    vue通过自定义指令 v-py 将名字转拼音
    ES6相关
    闭包
    apply和call与this
    RPC
    Linux根目录下各目录文件类型及各项缩写全称
    网站分析相关
    [转]作为首席架构师,我是如何选择并落地架构方案的?
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6292454.html
Copyright © 2011-2022 走看看