我使用两个 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
公共(public)类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,其中为位置属性设置多个 prop 文件和多个 propertyArray,如下所示。
<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 文件。
如果你不想有多个ServiceRecords,你可以使用Util方法..
public String getProperty(String serviceName, String propertyName) {
return propertySource.getProperty(serviceName+"."+propertyName);
}
关于java - 为什么多个 PropertyPlaceholderConfigurer 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20684518/