Multiple Spring PropertyPlaceholderConfigurer at the same time

I have two projects, one of which (Services) includes the second (Core). I defined this PropertyPlaceholderConfigurer below in the main project:

<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 Core placeholder in the top project, including appConfig.properties and some others. The only way I found is to define another different bean (different ID) at the top level and include new ones:

 <bean id="servicesPropertyConfigurer" class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:swagger.properties</value> </list> </property> </bean> 

But he gives that he cannot find appConfig.properties, I assume that he uses only one of these PropertyPlaceholderConfigurer at the same time? Do I need to specify all resources in the top 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 possible to use Core bean o in both cases at the same time?

+4
source share
2 answers

You can use several BUT at the same time, you must have different names / identifiers for each other, which will override the other. Next to this, you need to either use different placeholders ($ {...}) for each, or configure it to ignore unresolved placeholders.

Instead of using a class definition, I highly recommend using a namespace ( <context:property-placeholder .. /> , which will save you some xml.) It will generate beans with unique names so that you can have several at the same time, make sure you set the attribute ignore-unresolvable for true .

As a last resort, you can implement your BeanDefinitionRegistryPostProcessor , which detects all instances of PropertyPlaceHolderConfigurer , merges them into one, and moves all the locations / resources to the combined one. This way you don't have to worry about several different placeholders or unsolvable properties.

+12
source

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"/> 

+3
source

Source: https://habr.com/ru/post/1501285/


All Articles