码迷,mamicode.com
首页 > 编程语言 > 详细

spring 加载配置文件的相关配置总结

时间:2015-04-03 13:05:19      阅读:146      评论:0      收藏:0      [点我收藏+]

标签:

PropertyPlaceholderConfigurer
      注意: Spring容器仅允许最多定义一个PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的会被Spring忽略掉。
Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean
就会停止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了)。

一:配置单个Properties文件

 

技术分享
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location">
          <value>classpath:config/sysconfig/sysconfig.properties</value>
      </property>
  </bean>
View Code

 

技术分享
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="location" value="classpath:config/sysconfig/sysconfig.properties"/>
 </bean>
View Code

二:配置多个Properties文件

技术分享
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
       <list>
        <value>classpath:config/dbconfig/database.properties</value>
        <value>classpath:config/sysconfig/sysconfig.properties</value>
        <value>file:/opt/demo/config/demo-mq.properties</value>  
    </list>
     </property>
   </bean>
View Code

三:简化操作
为简化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。下面给出了配置示例,启用它后,开发者便不用配置PropertyPlaceholderConfigurer对象了。

技术分享
<context:property-placeholder location="classpath:jdbc.properties"/>
View Code

 

四:参考用例

1.

技术分享
1. applicationContext.xml
<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations" >
        <list>
            <value>classpath:config/dbconfig/database.properties</value>
            <value>classpath:config/sysconfig/sysconfig.properties</value>
        </list>
        </property>
    </bean> 
 // 其中classpath是引用src目录下的文件写法

<import resource="classpath:config/spring/spring-data.xml" /> 

spring-data.xml
<context:property-placeholder  properties-ref="deployProperties" />
View Code


2.

技术分享
applicationContext.xml
<context:property-placeholder location="classpath:config/*config/*.properties"/>   
View Code

3.

技术分享
applicationContext.xml
 <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations">
       <list>
        <value>classpath:config/dbconfig/database.properties</value>
        <value>classpath:config/sysconfig/sysconfig.properties</value>
        <value>file:/opt/demo/config/demo-mq.properties</value>  
    </list>
     </property>
   </bean>
View Code

4.

技术分享
applicationContext.xml
   <bean   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="order" value="1" />  
        <property name="ignoreUnresolvablePlaceholders" value="true" />   
        <property name="location" value="classpath:config/dbconfig/database.properties"/>  
    </bean> 
    <bean   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="order" value="2" />  
        <property name="ignoreUnresolvablePlaceholders" value="true" />   
        <property name="location" value="classpath:config/sysconfig/sysconfig.properties"/>  
    </bean> 
View Code

其中order属性代表其加载顺序,而ignoreUnresolvablePlaceholders为是否忽略不可解析的 Placeholder,如配置了多个PropertyPlaceholderConfigurer,则需设置为true

5.

技术分享
applicationContext.xml
  <bean id="dbResources" class="java.util.ArrayList">  
        <constructor-arg>  
        <list>
            <value>classpath:config/dbconfig/database.properties</value>
            <value>classpath:config/sysconfig/sysconfig.properties</value>
        </list>
        </constructor-arg>  
    </bean> 

<bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
      <property name="locations" ref="dbResources">
       
     </property>
   </bean>
View Code

6.

技术分享
applicationContext.xml
    <bean id="configproperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations" >
        <list>
            <value>classpath:config/dbconfig/database.properties</value>
            <value>classpath:config/sysconfig/sysconfig.properties</value>
        </list>
        </property>
   </bean> 
   <bean id="deployProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="properties" ref="configproperties">
       </property>
   </bean>  
View Code


7.context 标签和PropertyPlaceholderConfigurer 混合在一起要加ignoreUnresolvablePlaceholders

技术分享
<context:property-placeholder location="classpath:config/*config/database.properties"/>
 
 <bean   class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="order" value="2" />  
        <property name="ignoreUnresolvablePlaceholders" value="true" />   
        <property name="location" value="classpath:config/sysconfig/sysconfig.properties"/>  
    </bean>
View Code

 

纯属个人总结和网络搜索的资料

 

spring 加载配置文件的相关配置总结

标签:

原文地址:http://www.cnblogs.com/zjshd/p/4389450.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!