zoukankan      html  css  js  c++  java
  • Spring将classpath下的 .properties文件数据读出放到map中,在初始化时加载

    因为项目需要需要将配置文件中的键值对读出放到map中

    格式为:

    001=123456789

    Appcontext.xml中添加配置:

    <bean id="loadKeyFromProperties" class="com.;landau.init.LoadKeyFormProperties">
            <property name="keyFileResource">
                <value>classpath:keys.properties</value>
            </property>
        </bean>

    java代码:

    public class LoadKeyFormProperties implements InitializingBean {
    
        private Resource keyFileResource;
    
        private static Map<String, String> map = new HashMap<String, String>();
    
        protected static volatile boolean initialized = false;
    
        public static Map<String, String> getKey() {
            return map;
        }
    
        public void setKeyFileResource(Resource keyFileResource) {
            this.keyFileResource = keyFileResource;
        }
    
        /**
         * 将键值对取到集合内
         */
        private void loadKeyFormProperties() {
            if (initialized) {
                return;
            }
            InputStream is = null;
            try {
                is = keyFileResource.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String str = null;
                while ((str = br.readLine()) != null) {
                    String[] data = str.split("=");
                    map.put(data[0], data[1]);
                }
                initialized = true;
            } catch (Exception e) {
    
            } finally {
               is.close();
            }
    
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            loadKeyFormProperties();
        }
    
    }
  • 相关阅读:
    JS-BOM操作-Location、history、常用弹窗、屏幕属性
    JS的基础DOM操作-选取父子级元素、动态生成元素、修改元素、Classlist
    setup
    循环请求接口,统一处理
    多个url文件下载
    扁平数据结构转Tree
    es6 解构赋值
    watch与computed与props
    v-model与.sync组件通信
    v-on="$listeners"和v-bind="$attrs"
  • 原文地址:https://www.cnblogs.com/landauni/p/6878410.html
Copyright © 2011-2022 走看看