I’ve learnt that I can provide Spring with a <context:property-placeholder> element, which appears to use a PropertyPlaceholderConfigurer to interpolate variables (i.e. replace tokens like ${foo}) against a properties file.
Is there a way to customize the class that’s used for resolving the tokens? In particular, I’d quite like to use an Apache Commons Config Configuration object to provide the values of the tokens, rather than using a properties file.
Yes, subclass
PropertyPlaceholderConfigurerand override theloadPropertiesmethod. This method is passed aPropertiesobject which you can fill with values however you wish. Alternatively, you can define an alternative implementation ofPropertiesPersisterand plug that into a normalPropertyPlaceholderConfigurer.In Spring 3.1 you can pull values directly from your commons configuration object by creating your own
PropertySourceimplementation, and registering it with the application context, typically by using anApplicationContextInitializer. This blog post provides a good overview of the process.<context:property-placeholder>in Spring 3.1 will pull values from registeredPropertySourcesas well as from any properties files you specify.