我创建了带有两个PropertyPlaceholderConfigurer Bean的applicationContext,并且仅根据基于上下文对象的输入访问了一个。但是,当从“ Service2Record”实例访问属性时,我正在获取“ Service1Record”属性值。下面是我的示例代码。
ApplicationContext.xml
<beans >
<context:component-scan base-package="com.test.record" />
<!-- Service1 Properties files -->
<bean id="service1"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations" >
<value>classpath:service_1.properties</value>
</property>
<property name="properties" >
<value>service1.class=com.test.record.ServiceRecord</value>
</property>
</bean>
<bean id="service1record" class="${service1.class}" />
<!-- Service2 Properties files -->
<bean id="service2"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="locations">
<value>classpath:service_2.properties</value>
</property>
<property name="properties">
<value>service2.class=com.test.record.ServiceRecord</value>
</property>
</bean>
<bean id="service2record" class="${service2.class}" />
ServiceRecord Bean:-
@Configuration
公共类ServiceRecord {
@Value("${request_queue_name}")
private String requestQueueName;
@Value("${reply_queue_name}")
private String replyQueueName;
public String getRequestQueueName() {
return requestQueueName;
}
public String getReplyQueueName() {
return replyQueueName;
}
}
测试主要课程-
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"classpath:applicationContext.xml");
ServiceRecord serviceRecord = null;
String inputService = "SERVICE2";
if(inputService.equals("SERVICE1")){
serviceRecord = (ServiceRecord)context.getBean("service1record");
} else {
serviceRecord = (ServiceRecord)context.getBean("service2record");
}
System.out.println(" RequestQueueName : " + serviceRecord.getRequestQueueName());
}
和
service_1.properties
request_queue_name=SERVICE1.REQUEST
reply_queue_name = SERVICE1.REPLY
service_2.properties
request_queue_name=SERVICE2.REQUEST
reply_queue_name = SERVICE2.REPLY
在此,每次输出为“ RequestQueueName:SERVICE2.REQUEST”。您能否告诉您如何基于属性文件获取相应的值?
修改-
我有一个PPHC,在其中为location属性设置了多个prop文件,并为多个propertieArray设置了如下。
<property name="locations">
<list>
<value>classpath:common-service.properties</value>
<value>classpath:search-service.properties</value>
<value>classpath:update-service.properties</value>
</list>
</property>
<property name="propertiesArray" >
<list>
<value>common.class=com.xyz.rabbitmq.record.CommonUtil</value>
<value>search.class=com.xyz.rabbitmq.record.SearchRecord</value>
<value>update.class=com.xyz.rabbitmq.record.UpdateRecord</value>
</list>
</property>
<bean id="commonrecord" class="${common.class}"/>
<bean id="searchrecord" class="${search.class}"/>
<bean id="updaterecord" class="${update.class}"/>
在这里,每个属性文件中的键都不同,并且根据搜索或更新请求类型来获取Bean实例。
serviceRecord =(ServiceRecord)context.getBean(“ searchrecord”);
这种方法是否适合加载不同的文件?
最佳答案
我建议您在属性文件中使用不同的键:service_1.properties
service1.request_queue_name=SERVICE1.REQUEST
service1.reply_queue_name=SERVICE1.REPLY
service_2.properties
service1.request_queue_name=SERVICE2.REQUEST
service1.reply_queue_name=SERVICE2.REPLY
而不同的ServiceRecord文件
ServiceRecord1.java
ServiceRecord2.java
每个都从相关属性文件中读取属性。即,当添加新的属性集/文件时,您需要添加新的ServiceRecord文件。如果您不想有多个ServiceRecord,则可以改用Util方法..
public String getProperty(String serviceName, String propertyName) {
return propertySource.getProperty(serviceName+"."+propertyName);
}