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

Prevent beans to be instantiated at build-time unless it has been flagged #28838

Closed
snicoll opened this issue Jul 18, 2022 · 3 comments
Closed
Labels
status: declined A suggestion or change that we don't feel we should currently apply theme: aot An issue related to Ahead-of-time processing type: enhancement A general enhancement

Comments

@snicoll
Copy link
Member

snicoll commented Jul 18, 2022

It's becoming more apparent that certain beans will have to be instantiated at build time (typically to introspect the outcome of something that could have been overridden by the user). We want to make sure that these are limited and that trying to resolve too much dependencies will fail fast.

One idea was to adapt ContextAnnotationAutowireCandidateResolver to only resolve low-level dependencies, such as ApplicationContext, BeanFactory, and the like.

@snicoll snicoll added type: enhancement A general enhancement theme: aot An issue related to Ahead-of-time processing labels Jul 18, 2022
@snicoll snicoll added this to the 6.0.0-M6 milestone Jul 18, 2022
@snicoll snicoll self-assigned this Jul 26, 2022
@snicoll snicoll changed the title Restrict conditions that allow a bean to be instantiated at build-time Prevent beans to be instantiated at build-time unless it has been flagged Jul 28, 2022
@snicoll
Copy link
Member Author

snicoll commented Jul 28, 2022

Unfortunately, this approach is too restricted for configuration classes or builder-style that gathers a number of dependencies to produce the target bean. An example of such a use case is LocalContainerEntityManagerFactoryBean that is configured with various components, including a DataSource.

Rather than restricting the dependencies that one can inject, perhaps we could provide a proxy that would throw an exception if a component attempt to use it. Such proxy would not be created for classes that are flagged (maybe with an annotation). A list of types or predicates can also be provided.

snicoll added a commit to snicoll/spring-framework that referenced this issue Aug 1, 2022
@snicoll snicoll modified the milestones: 6.0.0-M6, 6.x Backlog Aug 10, 2022
@snicoll
Copy link
Member Author

snicoll commented Aug 10, 2022

After a review, we're not sure if we want to introduce this. We should see how far we go with the integration of the portfolio and how common such problem arises. We could argue that we may want to do the opposite in the end (flag a bean that has to be instantiated but we want to pass a proxy rather than actual instantiating it). I've moved the issue to the backlog as we're not committed to implement it for the next milestone.

@snicoll
Copy link
Member Author

snicoll commented Sep 26, 2022

Let's close this one for now. We can reopen if necessary.

@snicoll snicoll closed this as not planned Won't fix, can't repro, duplicate, stale Sep 26, 2022
@snicoll snicoll added the status: declined A suggestion or change that we don't feel we should currently apply label Sep 26, 2022
@snicoll snicoll removed this from the 6.x Backlog milestone Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: declined A suggestion or change that we don't feel we should currently apply 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