天天看点

spring读取properties文件

  1. PropertyPlaceholderConfigurer引入配置文件
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <!--采用通配符方式-->
            <value>classpath:system_config*.properties</value>
            <!--
            <value>classpath:system_config.properties</value>
            <value>file:system_config2.properties</value>
            -->
        </list>
    </property>
</bean>           

此方式,在java代码中使用

@Value( "${app.domain}") //properties中的key
private String 任意变量名;
           

2.util:properties引入配置文件

ps-使用util:properties必须要引入xml头文件

xmlns:util="http://www.springframework.org/schema/util   xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"
           
<!--ps:不允许使用通配符*-->
<util:propertiesid="config1"location="classpath:system_config.properties"/>           

此方式,在java代码中使用

@Value( "#{config1[app.domain]}") //properties中的key
private String 任意变量名;
           

3.PropertyPlaceholderConfigurer 结合 util:properties

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" ref="locationsList"/>
    <!-- order属性值从1开始计数,
      可以不设置,不设置表示计入全部 -->
    <property name="order" value="1"/>
</bean>

<util:list id="locationsList">
    <value>classpath:system_config.properties</value>
    <value>classpath:system_config2.properties</value>
</util:list>
           

整合后,调用属性, 只能使用

@Value( "${app.domain}") //properties中的key
private String 任意变量名;           

4.重现重复的属性配置:

1)使用PropertyPlaceholderConfigurer,

a.先从文件列表中找到拥有该属性配置的,最长的文件

b.再找到该文件该属性的最后一次配置。

2)使用util:propertiesid

a.直接找到该文件该属性的最后一次配置。

5.file文件引入(tomcat/bin目录) – 这样做的目的是隔离开发环境和发布环境的配置文件、将差异化配置放到war以外、方便发布

同classpath文件类似,

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
        <list>
            <value>classpath*:system_config.properties</value>
            <value>file:system_config2.properties</value>
        </list>
    </property>
</bean>           

6.PropertyPlaceholderConfigurer扩展

有这样一个问题,我每用一次配置,就要声明一个局部变量,如何能解决呢。

方法(一):定义一个配置文件集合的java bean .

提供一个getter(),不需要setter()

spring读取properties文件

方法(二):重写PropertyPlaceholderConfigurer

此部分参照引用: http://www.cnblogs.com/Gyoung/p/5507063.html
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<String, String>();
        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.**.PropertyPlaceholder">
        <property name="location">
            <value>classpath:config.properties</value>
        </property>
    </bean>`           

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

PropertyPlaceholder.getProperty("sql.name");