spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

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

Open snicoll opened this issue 1 year ago • 2 comments

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 avatar Jul 18 '22 16:07 snicoll

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 avatar Jul 28 '22 06:07 snicoll

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 avatar Aug 10 '22 13:08 snicoll

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

snicoll avatar Sep 26 '22 12:09 snicoll