<bean id="TSMConsumer" class="TSMConsumer" init-method="initConsumer" lazy-init="true">
<property name="responseQueueName" value=" ${tsmadapter.ems.responsequeuename}"></property>
<property name="factory" ref="TSMJmsConnectionFactory"></property>
<property name="instanceName" value="$ {tsm.instance}"></property>
</bean>
<property name="responseQueueName" value=" ${tsmadapter.ems.responsequeuename}"></property>
<property name="factory" ref="TSMJmsConnectionFactory"></property>
<property name="instanceName" value="$ {tsm.instance}"></property>
</bean>
上面spring配置中的变量从 test.properties文件中获取。
一种方式是在spring中配置直接加载properties文件,如下:
<context:property-placeholder location="classpath:test.properties" />
这样加载需要在保证beans中存在context
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd "
第二种方式是将test.properties放在 System.properties 中,java代码如下:
Properties properties = new Properties();
properties.load(this.getClass().getClassLoader().getResourceAsStream("test.properties"));
properties.putAll(System.getProperties());
System.setProperties(properties);
properties.putAll(System.getProperties());
System.setProperties(properties);
spring需要加的配置为:
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"></bean>
这样变量就会从system.properties中获取,上面红色背景的一定不能少,因为system.properties中本来存放这很多配置信息,如果不添加进去,就会出错。
转载于:https://blog.51cto.com/angelgirl/1073719