Spring加载配置文件总结

一般在项目中为了便于维护所以配置都要独立出来,写到配置文件中xx.properties中,现在总结下加载方式
1.简洁方式加载

<context:property-placeholder location="classpath:cfg.properties,classpath:cfg2.properties"/>

这种方式可以一次性把所以的配置文件一次加载进入
2.使用p标签加载方式

 <bean class="com.web.utils.EncryptPropertyPlaceholderConfigurer"
          p:location="classpath:xx.properties" p:fileEncoding="utf-8"/>

这种方式其中EncryptPropertyPlaceholderConfigurer是PropertyPlaceholderConfigurer的子类可以进行配置文件数据加密处理,当然可可以不加密处理

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"
          p:location="classpath:db.properties" p:fileEncoding="utf-8"/>

当需要进行加载多个配置文件时

  <bean class="com.it.spring.utils.EncryptPropertyPlaceholderConfigurer"
          p:locations="classpath*:x1.properties,x2.properties"
          p:fileEncoding="utf-8" p:ignoreUnresolvablePlaceholders="true"/>

3.原始方式加载

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

如果须有加载多个配置文件时:

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

第二种方式和第三中方式其实质是一样的,如果需要在不同 地方使用PropertyPlaceholderConfigurer加载配置文件,一定要在加入

<property name="ignoreUnresolvablePlaceholders" value="true" />

ignoreUnresolvablePlaceholders为是否忽略不可解析的Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值