Java 同时使用多个 Spring PropertyPlaceholderConfigurer
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18697050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple Spring PropertyPlaceholderConfigurer at the same time
提问by Amin Abu-Taleb
I have two projects, one of them (Services) includes the second one (Core). I've defined this PropertyPlaceholderConfigurer below within Core project:
我有两个项目,其中一个(服务)包括第二个(核心)。我在 Core 项目中定义了这个 PropertyPlaceholderConfigurer:
<bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:appConfig.properties</value>
</list>
</property>
</bean>
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:
而且我想在上层项目中扩展Core占位符,包括appConfig.properties和其他一些。我发现的唯一方法是在上层定义另一个不同的 bean(不同的 ID)并包含新的 bean:
<bean id="servicesPropertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:swagger.properties</value>
</list>
</property>
</bean>
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?:
但它会导致它找不到 appConfig.properties,我猜它同时只使用这些 PropertyPlaceholderConfigurer 之一?是否需要在上层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>
Is it posible to extend the Core bean o use both at the same time?
是否可以扩展 Core bean 或同时使用两者?
采纳答案by M. Deinum
You can use multiple at the same time BUTyou 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.
您可以同时使用多个,但您必须为每个名称/ID 使用不同的名称/ID,否则一个将覆盖另一个。接下来,您需要为每个占位符使用不同的占位符 (${...}) 或将其配置为忽略未解析的占位符。
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
.
我强烈建议<context:property-placeholder .. />
您不要使用类定义,而是使用名称空间(它可以为您节省一些 xml。它会生成具有唯一名称的 bean,因此您可以同时拥有多个 bean,确保将ignore-unresolvable
属性设置为true
.
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.
作为最后的手段,您可以实现BeanDefinitionRegistryPostProcessor
检测所有PropertyPlaceHolderConfigurer
实例的 a ,将它们合并为一个并将所有位置/资源移动到合并的一个。这样您就不必担心多个不同的占位符或无法解析的属性。
回答by Ugur Artun
try this code
试试这个代码
<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>
<property name="ignoreUnresolvablePlaceholders" value="true"/>