Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throw Exception when two primary beans are present in one factory #26612

Closed
wants to merge 1 commit into from

Conversation

nullzl
Copy link
Contributor

@nullzl nullzl commented Feb 26, 2021

Prior to this commit, the implementation of DefaultListableBeanFactory.determinePrimaryCandidate() did not throws NoUniqueBeanDefinitionException in the flowing two situations:

  • more than one primary bean in one ancestor factory.(test unit in DefaultListableBeanFactoryTests.autowireBeanByTypeWithTwoPrimaryCandidatesInOneAncestor())
  • more than one local primary bean which implements FactoryBean.(test unit in DefaultListableBeanFactoryTests.autowireBeanByTypeWithTwoPrimaryFactoryBeans())

This commit fix these issues by prefer primary bean in the youngest factory, and if the youngest factory has more than one primary bean thorws NoUniqueBeanDefinitionException.

Prior to this commit, the implementation of
`DefaultListableBeanFactory.determinePrimaryCandidate()` did not throws
`NoUniqueBeanDefinitionException` in the flowing two situations:
* more than one primary bean in one ancestor factory
* more than one local primary bean which implements `FactoryBean`

This commit fix these issues by prefer primary bean in the youngest factory,
and if the youngest factory has more than one primary bean
thorws `NoUniqueBeanDefinitionException`.
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Feb 26, 2021
@sbrannen sbrannen requested a review from jhoeller February 26, 2021 09:14
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Feb 26, 2021
@sbrannen sbrannen changed the title Throw Exception when tow primary beans in one factory Throw Exception when two primary beans are present in one factory Feb 26, 2021
@jhoeller jhoeller self-assigned this Aug 25, 2023
@jhoeller jhoeller added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Nov 23, 2023
@jhoeller jhoeller added this to the 6.2.x milestone Nov 23, 2023
@jhoeller
Copy link
Contributor

We are likely to implement this is in a different fashion but the general point is valid: We ignore multiple matching primary beans in ancestors, just taking the first one, whereas we do insist on a unique primary bean to be available locally. We might simply add another else clause to the present checks when we got a primaryBeanName already, rejecting another matching primary bean when it is not a local override.

@jhoeller jhoeller modified the milestones: 6.2.x, 6.2.0-M1 Jan 12, 2024
@jhoeller jhoeller closed this in 889c4e0 Feb 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants