我有一个Spring 3.1 @Configuration需要一个属性foo来构建一个bean.属性是在属性中定义的,defaults.properties但overrides.properties如果应用程序具有活动的overrideSpring配置文件,则可以由属性覆盖.
没有覆盖,代码看起来像这样,并且工作......
@Configuration
@PropertySource("classpath:defaults.properties")
public class MyConfiguration {
@Autowired
private Environment environment;
@Bean
public Bean bean() {
...
// this.environment.getRequiredProperty("foo");
...
}
}
Run Code Online (Sandbox Code Playgroud)
我想一个@PropertySource用于classpath:overrides.properties对队伍@Profile("overrides").有没有人对如何实现这一点有任何想法?我考虑过的一些选项是重复的@Configuration,但这会违反DRY或程序化操作ConfigurableEnvironment,但我不确定environment.getPropertySources.addFirst()调用的去向.
如果我直接注入属性@Value,但是在我使用Environment和getRequiredProperty()方法时,将以下内容放在XML配置中.
<context:property-placeholder ignore-unresolvable="true" location="classpath:defaults.properties"/>
<beans profile="overrides">
<context:property-placeholder ignore-unresolvable="true" order="0"
location="classpath:overrides.properties"/>
</beans>
Run Code Online (Sandbox Code Playgroud)
更新
如果您现在尝试执行此操作,请查看Spring Boot的YAML支持,尤其是"使用YAML而不是属性"部分.那里的配置文件支持会使这个问题没有实际意义,但还没有@PropertySource支持.
spring ×1