Closed
Description
Dave Syer opened SPR-7028 and commented
There are two ways I might want to do this, and both fail.
- Use
@ImportResource
in an@Configuration
to load an XML file, then override a bean using@Bean
- Write an XML file that imports another file and then defines a bean that is
@Configuration
, and the latter provides a bean with the same name
Neither works.
Case 1 fails because ConfigurationClassBeanDefinitionReader
always loads XML imports after the @Bean
definitions. This seems like the wrong order, so it would be good to understand why it is implemented that way (I imported before I defined the @Bean
, so I expect the latter to win).
This code in ConfigurationClassBeanDefinitionReader
prevents the override in case 2:
if (!(existingBeanDef instanceof ConfigurationClassBeanDefinition)) {
// no -> then it's an external override, probably XML
// overriding is legal, return immediately
return;
}
Affects: 3.0.1
Issue Links:
- Revised support for bean definition visibility and overriding [SPR-8189] #12839 Revised support for bean definition visibility and overriding ("is depended on by")
- Reconsider whether to generally allow bean overriding by name [SPR-10808] #15434 Reconsider whether to generally allow bean overriding by name
- Bean Configuration Overriding [SPR-5509] #10181 Bean Configuration Overriding
- Support profile exclusivity and/or bean definition finality [SPR-7982] #12637 Support profile exclusivity and/or bean definition finality
- Allow bean definitions from @Configuration classes to override those defined in XML [SPR-7341] #12000 Allow bean definitions from
@Configuration
classes to override those defined in XML
8 votes, 12 watchers