zoukankan      html  css  js  c++  java
  • Spring 读取classpath下的文件存到map里面

    Spring配置文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:aop="http://www.springframework.org/schema/aop"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:util="http://www.springframework.org/schema/util"
         xsi:schemaLocation="
       http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/tx 
       http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/util
       http://www.springframework.org/schema/util/spring-util-2.5.xsd">
    
        <description>应用组件</description>
    
        <!-- Configurer that replaces ${...} placeholders with values from a properties file -->
        <bean id="propertyConfigurer" class="com.jiewen.commons.toolkit.config.XmlExtPropertyPlaceholderConfigurer">
            <property name="extLocation" value="classpath:config.xml"/>
        </bean>
        
        <bean id="loadKeyFromProperties" class="com.test.init.LoadKeyFormProperties">
            <property name="keyFileResource">
                <value>classpath:keys.properties</value>
            </property>
        </bean>
      

        <!-- DataSource -->
        <bean id="dataSource" class="com.jiewen.commons.toolkit.db.BasicDataSource" destroy-method="close">
        <property name="driverClassName"><value>${config.jdbc.driver}</value></property>
        <property name="url"><value>${config.jdbc.url}</value></property>
        <property name="username"><value>${config.jdbc.username}</value></property>
        <property name="password"><value>${config.jdbc.password}</value></property>
        <property name="initialSize"><value>5</value></property>
        <property name="maxActive"><value>100</value></property>
        <property name="maxWait"><value>10000</value></property>
        <property name="minIdle"><value>5</value></property>
        <property name="validationQuery"><value>select 1 from dual</value></property>
        </bean>

            
    
    </beans>

    classpath下共有两个文件一个是数据库配置,一个是参数配置这里主要说参数配置文件里面的参数的获取:

    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.beans.factory.InitializingBean;
    import org.springframework.core.io.Resource;
    
    import com.jiewen.commons.util.IOUtil;
    
    /**
     * 初始化加载密钥文件
     * 
     * @author P.M
     * 
     */
    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;
        }
    
        /**
         * 将文件读取到map里面
         */
        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 {
                IOUtil.closeQuietly(is);
            }
    
        }
    
        @Override
        public void afterPropertiesSet() throws Exception {
            loadKeyFormProperties();
        }
    
    }
  • 相关阅读:
    PHP中获取当前页面的完整URL
    phpStydy配置memcache扩展
    mac 安装 php nginx mysql
    mysql分表的3种方法
    Apache虚拟主机配置
    Mysql命令行基本操作
    优化数据库对象
    select,epoll的比较
    lucene合并测试的总结
    验证相关度排序是否受查询的多个关键字在内容中相邻紧密程度有关
  • 原文地址:https://www.cnblogs.com/landauni/p/6763471.html
Copyright © 2011-2022 走看看