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

       在Spring项目中,你可能需要从properties文件中读入配置注入到bean中,例如数据库连接信息,memcached server的地址端口信息等,这些配置信息最好独立于jar包或者war包,这样便于修改配置。Spring提供了PropertyPlaceholderConfigurer类来处理这件事情。

一个系统中通常会存在如下一些以Properties形式存在的配置文件
1.数据库配置文件demo-db.properties:
Properties代码  
database.url=jdbc:mysql://localhost/smaple  
database.driver=com.mysql.jdbc.Driver  
database.user=root  
database.password=123  
 
2.消息服务配置文件demo-mq.properties:
Properties代码  
mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory  
mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000  
mq.java.naming.security.principal= jjktyjydjd 
mq.java.naming.security.credentials= jytjtyjjt
jms.MailNotifyQueue.consumer=5  
 
3.远程调用的配置文件demo-remote.properties:
Properties代码  
remote.ip=localhost  
remote.port=16800  
remote.serviceName=test  
一、系统中需要加载多个Properties配置文件
应用场景:Properties配置文件不止一个,需要在系统启动时同时加载多个Properties文件。
配置方式:
Xml代码  
  (1)<!-- 将多个配置文件读取到容器中,交给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>

      (2) 

<!-- 将多个配置文件读取到容器中(配置文件放在了tomcat中的),交给Spring管理 -->

      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>file:${catalina.home}/etc/domain/domain.properties</value>
                <value>file:${catalina.home}/etc/dcas/dcas-client.properties</value>
<value>file:${catalina.home}/etc/fruadmetrix/fruadmetrix.properties</value>
            </list>
        </property>
    </bean>

   <!--使用配置的另一种写法>

   <bean id="freightConfig" class="com.olymtech.dzg.freight.enquiry.rest.config.FreightConfig">
     <property name="fraudmetrixUrl" value="${fraudmetrix.fraudmetrixUrl}"></property>
    <property name="partner_code" value="${fraudmetrix.partner_code}"></property>
    <property name="secret_key" value="${fraudmetrix.secret_key}"></property>
    <property name="event_id" value="${fraudmetrix.event_id}"></property>
      </bean>

 (3)也可以将list抽取出来

            <!-- 将多个配置文件位置放到列表中 -->  

    <bean id="propertyResources" class="java.util.ArrayList">  
     <constructor-arg>  
       <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>  
     </constructor-arg>  
    </bean>   
    <!-- 将配置文件读取到容器中,交给Spring管理 -->  
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations" ref="propertyResources" />  
    </bean>  

 

原文地址:https://www.cnblogs.com/vanl/p/5362931.html