Spring用代码来读取properties文件

我们都知道,Spring可以@Value的方式读取properties中的值,只需要在配置文件中配置org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location">
          <value>classpath:config.properties</value>
      </property>
 </bean>

那么在需要用到这些获取properties中值的时候,可以这样使用

    @Value("${db.name}")
    private String dbName;

但是这有一个问题,我每用一次配置文件中的值,就要声明一个局部变量。有没有用代码的方式,直接读取配置文件中的值。

答案就是重写PropertyPlaceholderConfigurer

package com.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * Created by yd on 2017/3/30.
 */
public class PropertyPlaceholder extends PropertyPlaceholderConfigurer{
    private static Map<String,String> propertyMap;

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);
        propertyMap = new HashMap<>();
        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            propertyMap.put(keyStr, value);
        }
    }

    //static method for accessing context properties
    public static Object getProperty(String name) {
        return propertyMap.get(name);
    }
}

在配置文件中,用上面的类,代替PropertyPlaceholderConfigurer

 <bean id="propertyConfigurer" class="com.gyoung.mybatis.util.PropertyPlaceholder">
     <property name="location">
         <value>classpath:config.properties</value>
     </property>
 </bean>

这样在代码中就可以直接用编程方式获取

 PropertyPlaceholder.getProperty("db.name");

如果是多个配置文件,配置locations属性

     <!--将多个配置文件读取到容器中,交给Spring管理-->
    <bean id="propertyConfigurer" class="com.utils.PropertyPlaceholder">
        <property name="locations">
            <list>
                <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->
                <value>classpath:/properties/*.properties</value>
                <value>classpath*:*.properties</value>
                <!--<value>classpath:/properties/mongodb.properties</value>-->
            </list>
        </property>
    </bean>

猜你喜欢

转载自blog.csdn.net/weixin_36586564/article/details/79761364