Spring 2.5 annotation is pretty useful however it makes PropertyPlaceholderConfigurer unusable.
In all our projects we heavily used PropertyPlaceholderConfigurer to inject configuration values via constructor injection to configure our object stack. We did run in problems using PropertyPlaceholderConfigurer in our latest project using spring 2.5 and autowire injection. Since we useally run our projects ins different deployment modes (local, test, live) we want to switch configuration values based on a system property.
We end up with a small hack I want to share with those that search for it with any search engine.
We basically created a object we added to our context.xml that implements the BeanFactoryPostProcessor. Within the postProcessBeanFactory method we check the deployment mode property and based on that we load the property file. Than we we deploy peropty by property as a bean of type String, Integer or Boolean with the property key name as bean name. This looks for example like this:
RootBeanDefinition rbd = new RootBeanDefinition();
rbd.setAbstract(false);
rbd.setLazyInit(true);
rbd.setAutowireCandidate(true);
ConstructorArgumentValues constructorArgumentValues = new ConstructorArgumentValues();
rbd.setConstructorArgumentValues(constructorArgumentValues);
boolean success = false;
try {
int parseInt = Integer.parseInt(value);
constructorArgumentValues.addIndexedArgumentValue(0,
parseInt);
rbd.setBeanClass(Integer.class);
registry.registerBeanDefinition(keyName, rbd);
success = true;
} catch (Exception e) {
// nothing to do
}
Unfortunately Spring 2.5 annotation autowireing (see spring jira) does not support primitives. So we had to use non primitives in the constructor as well. However it worked pretty well. For example for the properties “maxHitsPerPage” and “maxPagesToShow” a constructor looks like this:
public UsersController(IDesignDao designDao, IUserDao userDao, @Qualifier("maxHitsPerPage")
Integer hitsPerPage, @Qualifier("maxPagesToShow")
Integer pagesToShow) {
Where “maxHitsPerPage” and “maxPagesToShow” are the property key names in our configuration files. Hope that helps a little, shoot me a mail if you need more sample code.