Spring中配置和读取多个Properties文件

一、系统中需要加载多个Properties配置文件

应用场景:Properties配置文件不止一个,需要在系统启动时同时加载多个Properties文件。

配置方式:

	  <!-- 将多个配置文件读取到容器中,交给Spring管理 -->  
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
           <list>  
              <!-- 这里支持多种寻址方式:classpath和file -->  
              <value>classpath:/opt/demo/config/demo-db.properties</value>  
              <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->  
              <value>file:/opt/demo/config/demo-mq.properties</value>  
              <value>file:/opt/demo/config/demo-remote.properties</value>  
            </list>  
        </property>  
    </bean>  
      
    <!-- 使用MQ中的配置 -->  
    <bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">  
        <property name="environment">  
            <props>  
                <prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>  
                <prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>  
                <prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>  
                <prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>  
                <prop key="userName">${mq.java.naming.security.principal}</prop>  
                <prop key="password">${mq.java.naming.security.credentials}</prop>  
            </props>  
        </property>  
    </bean>  

二、整合多工程下的多个分散的Properties

应用场景:工程组中有多个配置文件,但是这些配置文件在多个地方使用,所以需要分别加载。
配置如下:

  <!-- 将DB属性配置文件位置放到列表中 -->  
    <bean id="dbResources" class="java.util.ArrayList">  
        <constructor-arg>  
        <list>  
            <value>file:/opt/demo/config/demo-db.properties</value>  
        </list>  
        </constructor-arg>  
    </bean>  
  
    <!-- 将MQ属性配置文件位置放到列表中 -->  
    <bean id="mqResources" class="java.util.ArrayList">  
        <constructor-arg>  
        <list>  
            <value>file:/opt/demo/config/demo-mq.properties</value>  
        </list>  
        </constructor-arg>  
    </bean>  
      
    <!-- 用Spring加载和管理DB属性配置文件 -->  
    <bean id="dbPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="order" value="1" />  
        <property name="ignoreUnresolvablePlaceholders" value="true" />   
        <property name="locations" ref="dbResources" />  
    </bean>  
      
    <!-- 用Spring加载和管理MQ属性配置文件 -->  
    <bean id="mqPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="order" value="2" />  
        <property name="ignoreUnresolvablePlaceholders" value="true" />   
        <property name="locations" ref="mqResources" />  
    </bean>  
      
    <!-- 使用DB中的配置属性 -->  
    <bean id="rmsDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"   
        p:driverClassName="${demo.db.driver}" p:url="${demo.db.url}" p:username="${demo.db.username}"   
        p:password="${demo.db.password}" pp:maxActive="${demo.db.maxactive}"p:maxWait="${demo.db.maxwait}"   
        p:poolPreparedStatements="true" p:default>  
    </bean>  
      
    <!-- 使用MQ中的配置 -->  
    <bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">  
        <property name="environment">  
            <props>  
                <prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>  
                <prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>  
                <prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>  
                <prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>  
                <prop key="userName">${mq.java.naming.security.principal}</prop>  
                <prop key="password">${mq.java.naming.security.credentials}</prop>  
            </props>  
        </property>  
    </bean> 

	


三、Bean中直接注入Properties配置文件中的值

应用场景:Bean中需要直接注入Properties配置文件中的值 。

	<!-- 这种加载方式可以在代码中通过@Value注解进行注入, 可以将配置整体赋给Properties类型的类变量,也可以取出其中的一项赋值给String类型的类变量 -->
	<!-- <util:properties/> 标签只能加载一个文件,当多个属性文件需要被加载的时候,可以使用多个该标签 -->
	<util:properties id="remoteSettings"
		location="file:/opt/demo/config/demo-remote.properties" />

	<!-- <util:properties/> 标签的实现类是PropertiesFactoryBean, 直接使用该类的bean配置,设置其locations属性可以达到一个和上面一样加载多个配置文件的目的 -->
	<bean id="settings"
		class="org.springframework.beans.factory.config.PropertiesFactoryBean">
		<property name="locations">
			<list>
				<value>file:/opt/rms/config/rms-mq.properties</value>
				<value>file:/opt/rms/config/rms-env.properties</value>
			</list>
		</property>
	</bean>


public class Client() {  
    @Value("#{remoteSettings['remote.ip']}")  
    private String ip;  
    @Value("#{remoteSettings['remote.port']}")  
    private String port;  
    @Value("#{remoteSettings['remote.serviceName']}")  
    private String service;  
}  

四、Bean中存在Properties类型的类变量

应用场景:当Bean中存在Properties类型的类变量需要以注入的方式初始化
1. 配置方式:我们可以用(三)中的配置方式,只是代码中注解修改如下
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.beans.factory.annotation.Autowired;  
  
public class Client() {  
    @Value("#{remoteSettings}")  
    private Properties remoteSettings;  
} 



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值