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

  • 32271

Comment From: jhoeller

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.

Comment From: sbrannen

Very nice!

I see you also updated DynamicPropertySourceBeanInitializer accordingly.

Thanks 👍