we were using spring 2.5, we want to add make sure that my properties should be either provided from the environment config_path=C:/application.properties or overrides from default location that is classpath
So we did it as below
applicationcontext.xml
<bean class="com.test.utils.ExtendedPropertySourcesPlaceholderConfigurer">
<property name="overridingSource" value="file:${config_path}/application.properties"/>
<property name="locations" value="classpath*:META-INF/*-config.properties" />
</bean>
ExtendedPropertySourcesPlaceholderConfigurer code
public class ExtendedPropertySourcesPlaceholderConfigurer extends PropertySourcesPlaceholderConfigurer implements InitializingBean, ApplicationContextAware {
private ApplicationContext applicationContext;
private Resource overridingSource;
public void setOverridingSource(Resource overridingSource) {
this.overridingSource = overridingSource;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void afterPropertiesSet() throws Exception {
MutablePropertySources sources = ((ConfigurableApplicationContext) applicationContext).getEnvironment().getPropertySources();
if (overridingSource == null) {
return;
}
sources.addFirst(new ResourcePropertySource(overridingSource));
}
}
Now we are moving this to spring 3.1.2 and can help me out weather spring has provided some new API to do it more efficient way?
Spring 3.1 introduced a new Environment Abstraction, PropertySource Abstraction (both links show SpringSource Blog articles).
But I think you do not need to override it in spring 3.0 or 3.1
BTW. in Spring 3.0 the bean class is: PropertyPlaceholderConfigurer (see this blog http://www.baeldung.com/2012/02/06/properties-with-spring/)