我有两个项目,其中一个(服务)包括第二个项目(核心)。我在Core项目中定义了这个PropertyPlaceholderConfigurer:
<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)并包含新的bean:
<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中指定所有资源?:
<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是否可以同时使用?
答案 0 :(得分:12)
你可以同时使用多个但是你必须为每个人使用不同的名字/ ids,一个人会覆盖另一个人。接下来,您需要为每个占位符使用不同的占位符($ {...}),或者将其配置为忽略未解析的占位符。
而不是使用类定义我强烈建议您使用命名空间(<context:property-placeholder .. />
,它可以节省一些xml。(它将生成具有唯一名称的bean,因此您可以同时拥有多个,确保设置ignore-unresolvable
属性为true
。
作为最后的手段,您可以实现一个BeanDefinitionRegistryPostProcessor
来检测所有PropertyPlaceHolderConfigurer
个实例,将它们合并为一个并将所有位置/资源移动到合并的实例。这样您就不必担心多个不同的占位符或无法解析的属性。
答案 1 :(得分:3)
试试这段代码
<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"/>