mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000
一、系统中需要加载多个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>