You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Ignore identical BeanDefinition registration when allowBeanDefinitionOverriding is false #27978
in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement
Calling DefaultListableBeanFactory.registerBeanDefinition() twice with the same bean name and same or equal BeanDefinition object results in an exception if allowBeanDefinitionOverriding was set to false (as is the default in Spring Boot 2.1+).
The javadoc for DefaultListableBeanFactory.setAllowBeanDefinitionOverriding specifically defines (emphasis is mine):
Set whether it should be allowed to override bean definitions by registering a different definition with the same name, automatically replacing the former...
Practically, this makes splitting a monolith into modules and libraries a lot harder as it bans common bean definitions due to recursive imports, e.g.:
Module 1 uses db hence @Import(DbConfig.class).
Module 2 uses db and also @Import(DbConfig.class).
Service 3 uses both module 1 and 2 and fails to load because of the double import if allowBeanDefinitionOverriding=false.
Allowing for silent-ignore of identical definitions will ease transitions from "one large context" to smaller modules that can be self-descriptive in their dependencies.
changed the title [-]Bean redefinition protection triggers on identical BeanDefinition objects[/-][+]Ignore identical BeanDefinition registration when allowBeanDefinitionOverriding is false[/+]on Jan 26, 2022
changed the title [-]Ignore identical BeanDefinition registration when allowBeanDefinitionOverriding is false[/-][+]Ignore identical `BeanDefinition` registration when `allowBeanDefinitionOverriding` is `false`[/+]on Feb 1, 2023
As far as I was able to investigate this, this is only a problem with programmatic registrations and external configuration formats such as XML, whereas double @Import usage works fine since the configuration class parser explicitly merges multiple imports for the same target class already. This should actually cover the scenario that you describe?
In general, we aim to explicitly skip re-registration (such as in the configuration class parser) rather than silently swallow it in DefaultListableBeanFactory itself. From that perspective, I'm inclined to close this issue since we do not intend to revisit the factory-level registry behavior there. If there is a concrete @Configuration/@Import scenario where this does not work as intended, feel free to reopen this issue with a corresponding test or to raise a separate issue for the specific target scenario.
in: coreIssues in core modules (aop, beans, core, context, expression)status: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement
Activity
[-]Bean redefinition protection triggers on identical BeanDefinition objects[/-][+]Ignore identical BeanDefinition registration when allowBeanDefinitionOverriding is false[/+]sbrannen commentedon Jan 26, 2022
Good point, @bironran.
We aim to loosen the restrictions (as indicated by the updated title for this issue) in
5.3.x.Thanks for bringing this up.
[-]Ignore identical BeanDefinition registration when allowBeanDefinitionOverriding is false[/-][+]Ignore identical `BeanDefinition` registration when `allowBeanDefinitionOverriding` is `false`[/+]jhoeller commentedon Jun 2, 2023
As far as I was able to investigate this, this is only a problem with programmatic registrations and external configuration formats such as XML, whereas double
@Importusage works fine since the configuration class parser explicitly merges multiple imports for the same target class already. This should actually cover the scenario that you describe?In general, we aim to explicitly skip re-registration (such as in the configuration class parser) rather than silently swallow it in
DefaultListableBeanFactoryitself. From that perspective, I'm inclined to close this issue since we do not intend to revisit the factory-level registry behavior there. If there is a concrete@Configuration/@Importscenario where this does not work as intended, feel free to reopen this issue with a corresponding test or to raise a separate issue for the specific target scenario.