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

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

一:配置单个Properties文件spring

 

<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对象了。app

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

 

四:参考用例ide

1.spa

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.3d

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

3.code

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.xml

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

 

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

相关文章
相关标签/搜索