Closed
Description
László Váradi opened SPR-9682 and commented
The ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForModelMethod contains the following:
// has this already been overridden (e.g. via XML)?
if (this.registry.containsBeanDefinition(beanName)) {
BeanDefinition existingBeanDef = registry.getBeanDefinition(beanName);
// is the existing bean definition one that was created from a configuration class?
if (!(existingBeanDef instanceof ConfigurationClassBeanDefinition)) {
// no -> then it's an external override, probably XML
// overriding is legal, return immediately
if (logger.isDebugEnabled()) {
logger.debug(String.format("Skipping loading bean definition for %s: a definition for bean " +
"'%s' already exists. This is likely due to an override in XML.", beanMethod, beanName));
}
return;
}
}
But it is not true ("overriding is legal") if the application context (AbstractRefreshableApplicationContext) is set to disallow it by setAllowBeanDefinitionOverriding(true).
I think the bean definition should be registered, and let the application context decide whether the override is legal or not. And if it is legal, which bean should be active?
In our case, classpath scanning have found the bean, and the bean definition in a @Configuration
class with the same name (but with more specific configuration) was not created, causing some problem.
Affects: 3.1.2
Issue Links:
- Cannot use @Primary to override @ComponentScan-ed beans [SPR-10795] #15421 Cannot use
@Primary
to override@ComponentScan-ed
beans - Beans defined by @ComponentScan are skipped in @Configuration override [SPR-9567] #14201 Beans defined by
@ComponentScan
are skipped in@Configuration
override
6 votes, 12 watchers