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

Introduce API to initialize BeanFactory before singleton instantiation #32836

Closed
sbrannen opened this issue May 16, 2024 · 2 comments
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@sbrannen
Copy link
Member

Overview

In order to properly implement #32271, I needed a way to eagerly initialize all beans created by @Bean factory methods annotated with @DynamicPropertySource before standard singleton beans are instantiated; however, Spring does not currently provide an official API that developers can implement to achieve that.

As a workaround, DynamicPropertySourceBeanInitializer temporarily implements LoadTimeWeaverAware (even though it has nothing to do with load-time weaving) since doing so is currently the only way to have a component eagerly initialized before ConfigurableListableBeanFactory's preInstantiateSingletons() phase.

Implementing LoadTimeWeaverAware to achieve this is arguably a hack, and in light of that we would like to introduce an official mechanism that allows developers to register components that are able to initialize the BeanFactory before the preInstantiateSingletons() phase (and possibly before LoadTimeWeaverAware components are initialized).

Proposal

@jhoeller has proposed a new BeanFactoryInitializer interface with an initializeBeanFactory(ConfigurableListableBeanFactory) callback, and we will investigate our options in the Spring Framework 6.2 M3 time frame.

Related Issues

@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement labels May 16, 2024
@sbrannen sbrannen added this to the 6.2.0-M3 milestone May 16, 2024
@jhoeller jhoeller self-assigned this May 16, 2024
@jhoeller jhoeller modified the milestones: 6.2.0-M3, 6.2.0-M4 May 22, 2024
@jhoeller
Copy link
Contributor

jhoeller commented Jun 4, 2024

This is available as a BeanFactoryInitializer interface with an initialize(ListableBeanFactory) method now, generically declarable for a more concrete type as well, analogous to the existing ApplicationContextInitializer interface. BeanFactoryInitializer beans get autodetected and automatically applied right before the initialization of LoadTimeWeaverAware beans.

@sbrannen
Copy link
Member Author

sbrannen commented Jun 5, 2024

Very nice!

I see you also updated DynamicPropertySourceBeanInitializer accordingly.

Thanks 👍

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

No branches or pull requests

2 participants