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()
constant

方法(二):重写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");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值