I am working on a project with several separate modules, each with their own application context properties files. I want to be able to load all these properties so that they can be used for placeholder resolution by Spring.
Previous questions have mentioned this and there is a good blog post here that describes how to use a PropertyPlaceholderConfigurer in each context, order them by priority and set ignoreUnresolveablePlaceholders to true so that these properties files can cross-reference each other without blowing up.
However, this doesn't solve my problem as I also want to be able to use the properties I am loading for some custom placeholder resolution (from some yaml files I am parsing). This requires using a PropertyPlaceholderHelper, which requires the Properties object as an argument.
As far as I can tell, potential solutions are:
1) Merge all the properties file into one properties bean. This can then be used to create a PropertyPlaceholderConfigurer (for Spring's internal placeholder resolution) and used with a PropertyPlaceholderHelper (for my own placeholder resolution)
2) Somehow configure a PropertyPlaceholderHelper to use the set of properties, and their hierarchical arrangement, held by the PropertyPlaceholderConfigurers if I go ahead and follow the advice of that blog post.
Unfortunately I can't work out how to do either of these. Any help would be greatly appreciated!
PS It looks like Spring 3.1 will be a big help here...unfortunately we aren't ready to move to it yet so I still need a solution to tide me over!
**** EDIT ****
Thanks for the answers so far. They are good answers but unfortunately won't help me because (and apologies for not mentioning this earlier) we are currently in the process of separating out the core modules of our project from the non-core modules. This means that the core modules, and their application context, cannot hard-code the names of the properties files. Frustratingly, Spring's classpath scanning appears to be broken, so wildcards of the type "classpath*:*.properties" only work when building the individual modules, not the top level project (I believe this is a known issue).
The question is then how to merge properties files defined in non-core modules into existing properties files defined in core modules. At the moment I am going ahead with a BeanPostProcessor - I am just wondering if there is a simpler/more elegant way to do this?
Thanks
Spring boot provides a lot of features out of the box. Having multiple application. properties files is one of them.
We use a singleton bean whose properties map to entries in 3 separate properties files. This is the main class to read properties from multiple properties files into an object in Spring Boot. We use @PropertySource and locations of the files in the classpath. The application.
Multiple Properties can be accessed in Spring by either, @PropertySource( {"name1", "name2"} )
Spring Boot supports different properties based on the Spring active profile. For example, we can keep two separate files for development and production to run the Spring Boot application.
You can collect multiple property files into one bean quite easily:
<bean id="allProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true"/>
<property name="ignoreResourceNotFound" value="true"/>
<property name="locations">
<list>
<value>classpath*:default.properties</value>
<value>classpath*:overrides.properties</value>
<value>file:${APP_HOME}/**/*.properties</value>
</list>
</property>
</bean>
This particular example will gather all default.properties, overrides.properties on classpath and property files in your APP_HOME. Now you can refer to this bean from ProperyPlaceholderConfigurer or your custom postprocessor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With