Java 同时配置多个弹簧属性PlaceHolder
我有两个项目,其中一个(服务)包括第二个(核心)。我在核心项目中定义了以下PropertyPlaceHolderConfigure:Java 同时配置多个弹簧属性PlaceHolder,java,spring,Java,Spring,我有两个项目,其中一个(服务)包括第二个(核心)。我在核心项目中定义了以下PropertyPlaceHolderConfigure: <bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="locations"> <list>
<bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:appConfig.properties</value>
</list>
</property>
</bean>
类路径:appConfig.properties
我想在上面的项目中扩展核心占位符,包括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>
类路径:swagger.properties
但它会产生无法找到appConfig.properties的结果,我猜它只是同时使用这些PropertyPlaceHolderConfiger中的一个?是否需要在上层PropertyConfigure中指定所有资源
<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>
类路径:swagger.properties
类路径:appConfig.properties
是否可以扩展核心bean以同时使用两者?试试下面的代码
<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"/>
类路径:swagger.properties
类路径:appConfig.properties
您可以同时使用多个但是您必须为每一个都有不同的名称/id,一个将覆盖另一个。接下来,您需要为每个文件使用不同的占位符(${…}),或者将其配置为忽略未解析的占位符 我强烈建议您不要使用类定义,而是使用名称空间(
),它可以为您保存一些xml。(它将生成具有唯一名称的bean,这样您可以同时拥有多个bean,请确保将ignore unsolvable
属性设置为true
)
最后,您可以实现aBeanDefinitionRegistryPostProcessor
,它检测所有PropertyPlaceHolderConfigure
实例,将它们合并到一个实例中,并将所有位置/资源移动到合并的实例中。这样您就不必担心多个不同的占位符或无法解决的属性