• 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初始化完毕======================>");
        }
    
    }
  • 相关阅读:
    c语言数组指针
    (4)activiti工作流引擎之uel表达式
    (3)activiti流程的挂起和激活
    (2)java程序走一遍工作流activiti
    (1)activiti认识以及数据库和插件配置
    linux 下路由配置
    lvs-dr+keepalived
    LVS-DR 配置测试
    简单认识TCP/IP协议
    mysql 主从同步-读写分离
  • 原文地址:https://www.cnblogs.com/shihaiming/p/6292454.html
Copyright © 2020-2023  润新知