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

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

Closed
snicoll opened this issue Jul 20, 2023 · 0 comments
Closed

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

snicoll opened this issue Jul 20, 2023 · 0 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) theme: aot An issue related to Ahead-of-time processing type: enhancement A general enhancement
Milestone

Comments

@snicoll
Copy link
Member

snicoll commented Jul 20, 2023

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.

@snicoll snicoll added type: enhancement A general enhancement theme: aot An issue related to Ahead-of-time processing labels Jul 20, 2023
@snicoll snicoll added this to the 6.x Backlog milestone Jul 20, 2023
@jhoeller jhoeller changed the title Allow prefered constructor to be defined at the BeanDefinition level Allow preferred constructor to be defined at the BeanDefinition level Jul 21, 2023
@jhoeller jhoeller self-assigned this Jul 21, 2023
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Jul 21, 2023
@jhoeller jhoeller modified the milestones: 6.x Backlog, 6.1.x, 6.1.0-M4 Jul 21, 2023
odrotbohm added a commit to spring-projects/spring-data-commons that referenced this issue Aug 1, 2023
@jhoeller jhoeller changed the title Allow preferred constructor to be defined at the BeanDefinition level Allow preferred constructor to be defined at the BeanDefinition level Aug 2, 2023
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) theme: aot An issue related to Ahead-of-time processing type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants