我们可以使用 <context:property-placeholder>
外部化属性我们可以通过配置 <context:property-override>
来覆盖 Spring bean 属性如下:
<context:property-placeholder location="classpath:application.properties"/>
<context:property-override location="classpath:override.properties"/>
我想将我的 XML 配置移动到 JavaConfig。
@Configuration
@ComponentScan
@PropertySource("classpath:application.properties")
public class AppConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
但是如何使用注释配置我的覆盖属性?
附言: 我有一个 bean 说 MyBean 如下:
@Component
public class MyBean {
@Value("${someProp}")
private String someProp;
}
在我的 application.properties
我有
someProp=TestValue
在我的 override.properties
我正在重写 someProp 值作为
myBean.someProp=RealValue
最佳答案
不,不是。
但是您可以在配置类中创建类型为 PropertyOverrideConfigurer
的 bean,结果相同。
更新
例如:
@Bean public static PropertyOverrideConfigurer propertyOverrideConfigurer() {
PropertyOverrideConfigurer overrideConfigurer = new PropertyOverrideConfigurer();
overrideConfigurer.setLocation(new ClassPathResource("override.properties"));
return overrideConfigurer;
}
注意 static 修饰符,这是因为 BFPP 应该在容器生命周期的早期实例化。
关于java - 什么是 Spring 的注解对应物 <context :property-override>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28718336/