简体   繁体   English

什么是Spring的Annotation对应物 <context:property-override> ?

[英]What is Annotation counterpart for Spring <context:property-override>?

We can externalize properties using <context:property-placeholder> and we can override the Spring bean properties by configuring <context:property-override> as follows: 我们可以使用<context:property-placeholder>外部化属性,我们可以通过配置<context:property-override>来覆盖Spring bean属性,如下所示:

<context:property-placeholder location="classpath:application.properties"/>
<context:property-override location="classpath:override.properties"/>

I want to move my XML config to JavaConfig. 我想将我的XML配置移动到JavaConfig。

@Configuration
@ComponentScan
@PropertySource("classpath:application.properties")
public class AppConfig {

    @Bean  
    public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {  
        return new PropertySourcesPlaceholderConfigurer();  
    }

}

But how to configure my override properties using Annotation? 但是如何使用Annotation配置覆盖属性?

PS: I have a bean say MyBean as follows: PS:我有一个bean说MyBean如下:

@Component
public class MyBean {

    @Value("${someProp}")
    private String someProp;
}

In my application.properties I have 在我的application.properties我有

someProp=TestValue

and in my override.properties i am overriding someProp value as 在我的override.properties我将someProp值覆盖为

myBean.someProp=RealValue

No, It isn't. 不,不是。

But you could create a bean of type PropertyOverrideConfigurer in the configuration class whit the same result. 但是您可以在配置类中创建一个PropertyOverrideConfigurer类型的bean,其结果相同。

Update 更新

For example: 例如:

@Bean public static PropertyOverrideConfigurer  propertyOverrideConfigurer() { 
    PropertyOverrideConfigurer overrideConfigurer = new PropertyOverrideConfigurer(); 
    overrideConfigurer.setLocation(new ClassPathResource("override.properties"));
    return overrideConfigurer; 
}

Note the static modifier, this is because BFPP should be instantiated early in the container lifecycle. 注意static修饰符,这是因为BFPP应该在容器生命周期的早期实例化。

see http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html for more info. 有关详细信息,请参阅http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM