Skip to content

Ignore identical BeanDefinition registration when allowBeanDefinitionOverriding is false #27978

Closed

Description

@bironran

Affects: 2.5.2 - 5.3, Spring Boot 2.1+

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.:

  1. Module 1 uses db hence @Import(DbConfig.class).
  2. Module 2 uses db and also @Import(DbConfig.class).
  3. 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.

This behavior was introduced in #9052.

Activity

  1. added
    status: waiting-for-triage
    on Jan 26, 2022
  2. added
    in: core
    type: enhancement
    on Jan 26, 2022
  3. modified the milestones: Triage Queue, 5.3.16 on Jan 26, 2022
  4. removed
    status: waiting-for-triage
    on Jan 26, 2022
  5. changed the title [-]Bean redefinition protection triggers on identical BeanDefinition objects[/-] [+]Ignore identical BeanDefinition registration when allowBeanDefinitionOverriding is false[/+] on Jan 26, 2022
  6. sbrannen commented on Jan 26, 2022

    @sbrannen
    Member

    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.

  7. modified the milestones: 5.3.16, 5.3.17 on Feb 16, 2022
  8. modified the milestones: 5.3.17, 5.3.x on Mar 15, 2022
  9. modified the milestones: 5.3.x, 6.1.x on Feb 1, 2023
  10. changed the title [-]Ignore identical BeanDefinition registration when allowBeanDefinitionOverriding is false[/-] [+]Ignore identical `BeanDefinition` registration when `allowBeanDefinitionOverriding` is `false`[/+] on Feb 1, 2023
  11. self-assigned this
    on Feb 14, 2023
  12. modified the milestones: 6.1.x, 6.1.0-M1 on Feb 14, 2023
  13. jhoeller commented on Jun 2, 2023

    @jhoeller
    Contributor

    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.

  14. removed this from the 6.1.0-M1 milestone on Jun 2, 2023
  15. added
    status: declined
    on Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: corestatus: declinedtype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions