多个Spring PropertyPlaceholderConfigurer同时出现

Ami*_*leb 4 java spring

我有两个项目,其中一个(服务)包括第二个项目(核心).我在Core项目中定义了下面的PropertyPlaceholderConfigurer:

<bean id="propertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
    <property name="locations">
         <list>
             <value>classpath:appConfig.properties</value>
         </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)

我想在上层项目中扩展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>
Run Code Online (Sandbox Code Playgroud)

但它产生它无法找到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>
Run Code Online (Sandbox Code Playgroud)

扩展Core bean是否可以同时使用?

M. *_*num 12

你可以同时使用多个但是你必须为每个人使用不同的名字/ ids,一个人会覆盖另一个.接下来,您需要为每个占位符使用不同的占位符($ {...}),或者将其配置为忽略未解析的占位符.

我没有使用类定义,而是强烈建议使用命名空间(<context:property-placeholder .. />这可以节省一些xml.(它将生成具有唯一名称的bean,因此您可以同时拥有多个,确保将ignore-unresolvable属性设置为true.

作为最后的手段,您可以实现一个BeanDefinitionRegistryPostProcessor检测所有PropertyPlaceHolderConfigurer实例的a,将它们合并为一个并将所有位置/资源移动到合并的实例.这样您就不必担心多个不同的占位符或无法解析的属性.


Ugu*_*tun 5

试试这个代码

<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"/>
Run Code Online (Sandbox Code Playgroud)