简体   繁体   中英

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 location="classpath:application.properties"/>
<context:property-override location="classpath:override.properties"/>

I want to move my XML config to 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?

PS: I have a bean say MyBean as follows:

@Component
public class MyBean {

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

In my application.properties I have

someProp=TestValue

and in my override.properties i am overriding someProp value as

myBean.someProp=RealValue

No, It isn't.

But you could create a bean of type PropertyOverrideConfigurer in the configuration class whit the same result.

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.

see http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Bean.html for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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