多个Spring PropertyPlaceholderConfigurer同时出现 [英] Multiple Spring PropertyPlaceholderConfigurer at the same time
问题描述
我有两个项目,其中一个(服务)包括第二个项目(核心)。我在Core项目中定义了下面的PropertyPlaceholderConfigurer:
I have two projects, one of them (Services) includes the second one (Core). I've defined this PropertyPlaceholderConfigurer below within Core project:
<bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:appConfig.properties</value>
</list>
</property>
</bean>
我想在上层项目中扩展Core占位符,包括appConfig.properties和其他一些。我找到的唯一方法是在上层定义另一个不同的bean(不同的ID)并包含新的:
And I want to extend the Core placeholder in the upper project, including appConfig.properties and some other ones. The only way I found is to define another different bean (different ID) in the upper level and include the new ones:
<bean id="servicesPropertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:swagger.properties</value>
</list>
</property>
</bean>
但它产生无法找到appConfig.properties,我想它只使用其中一个PropertyPlaceholderConfigurer同时?是否需要在上面的propertyConfigurer中指定所有资源?:
But it yields it cannot find appConfig.properties, I guess it's only using one of these PropertyPlaceholderConfigurer at the same time? Is it necesary to specify all resources in the upper propertyConfigurer?:
<bean id="servicesPropertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:swagger.properties</value>
<value>classpath:appConfig.properties</value>
</list>
</property>
</bean>
扩展Core bean是否可以同时使用两者?
Is it posible to extend the Core bean o use both at the same time?
推荐答案
你可以同时使用多个但是你必须为其他每个人都有不同的名字/ ids另一个。接下来,您需要为每个占位符使用不同的占位符($ {...}),或者将其配置为忽略未解析的占位符。
You can use multiple at the same time BUT you have to have different names/ids for each else one will override the other. Next to that you need to either use different placeholders (${...}) for each or configure it to ignore unresolved placeholders.
我强烈建议您使用命名空间(< context:property-placeholder .. />
为你节省了一些xml。(它将生成具有唯一名称的bean,因此你可以同时拥有多个,确保设置 ignore-unresolvable
属性到 true
。
Instead of using the class definition I strongly suggest yo use the namespace (<context:property-placeholder .. />
which saves you some xml. (It will generate beans with unique names so you can have multiple at the same time, make sure to set the ignore-unresolvable
attribute to true
.
作为最后的手段,你可以实现你的 BeanDefinitionRegistryPostProcessor
检测所有 PropertyPlaceHolderConfigurer
实例,将它们合并为一个并将所有位置/资源移动到合并的实例。这样您就不必担心多个不同的占位符或不可解析的属性。
As a last resort you could implement your a BeanDefinitionRegistryPostProcessor
which detects all PropertyPlaceHolderConfigurer
instances, merges them into one and move all the locations/resources to the merged one. That way you don't have to worry about multiple different placeholders or unresolvable properties.
这篇关于多个Spring PropertyPlaceholderConfigurer同时出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!