I have the following scenario:
<bean name="searchHelper" class="com.test.SearchHelperImpl"/>
What I was thinking of making is just copying the whole configuration into the new project and changing what needs to be changed, but that's not convenient and there must be an easier way of doing this.
My question is, how do I override the definition of the "searchHelper" bean to use SearchHelperBImpl instead of SearchHelperImpl? I want to use the same bean name in order for everything that uses this name to use the new implementation. I am using Spring 3.2.2
Thanks
@Buchi Yes, it is allowed. Bean from the file read later in sequence will override entirely previous definition which won't be instantiated at all.
To make a configuration in Spring Boot, you need to create a class and annotate it with @Configuration . Usually, in the configuration class, you can define a beans object. But if you want to override built-in configuration, you need to create a new class that extends the built-in class.
The decision to go with package-private visibility was made simply because private visibility is not allowed for @Bean methods due to CGLIB constraints.
You should be able to utilize the primary
xml attribute on your bean
element.
<bean name="searchHelper" primary="true" class="com.test.SearchHelperBImpl"/>
Alternatively, if you are using JavaConfig, you can utilize the @Primary
annotation.
@Primary
@Bean
public SearchHelper searchHelper() {
return new SearchHelperBImpl();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With