我们使用的是spring 2.5,我们要添加确保我的属性应该从环境中提供config_path=C:/application.properties
或从默认位置(即类路径)覆盖
所以我们做了如下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 代码
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));
}
}
现在我们将它移到 spring 3.1.2 并且可以帮助我判断 spring 提供了一些新的 API 来更有效地完成它吗?