Spring 读取classpath下的文件存到map里面

Posted LandauNi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
    }

}

 













以上是关于Spring 读取classpath下的文件存到map里面的主要内容,如果未能解决你的问题,请参考以下文章

spring读取classpath目录下的配置文件通过表达式去注入属性值.txt

Spring读取mybatis在多个jar包下的的mapper文件

什么是classpath

SpringBoot——web项目下读取classpath下的文件心得

springboot-项目获取resources下文件碰到的问题(classPath下找不到文件和文件名乱码)

classpath路径指什么