Skip to content

Allow preferred constructor to be defined at the BeanDefinition level #30917

Closed
@snicoll

Description

@snicoll
Member

See #30900 for an example.

When crafting a BeanDefinition for a class with multiple constructors and AUTOWIRE_CONSTRUCTOR mode, the regular runtime chooses the "most suitable" constructor based on the available beans in the BeanFactory. Arguably this is a behavior that can be a bit surprising and can be expressed in a much better way using Optional or ObjectProvider for the constructor arguments that may or may not be present.

AOT does not support this algorithm and we're not keen to change that as we're considering deprecating and removing the capability of choosing the constructor based on available dependencies. However, for existing use cases, users have no other choice than refactoring their code, such as adding @Autowired on the preferred constructor, or changing the signature to use Optional or ObjectProvider. This issue is about offering an escape hatch, for instance adding an attribute at the bean definition level that ConstructorResolver would use to pick up the preferred constructor.

Activity

added this to the 6.x Backlog milestone on Jul 20, 2023
changed the title [-]Allow prefered constructor to be defined at the BeanDefinition level[/-] [+]Allow preferred constructor to be defined at the BeanDefinition level[/+] on Jul 21, 2023
self-assigned this
on Jul 21, 2023
added
in: coreIssues in core modules (aop, beans, core, context, expression)
on Jul 21, 2023
changed the title [-]Allow preferred constructor to be defined at the BeanDefinition level[/-] [+]Allow preferred constructor to be defined at the `BeanDefinition` level[/+] on Aug 2, 2023
added a commit that references this issue on Sep 9, 2024
4786e2b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)theme: aotAn issue related to Ahead-of-time processingtype: enhancementA general enhancement

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @snicoll@jhoeller

      Issue actions

        Allow preferred constructor to be defined at the `BeanDefinition` level · Issue #30917 · spring-projects/spring-framework