Spring boot bean registration dependencies

  Kiến thức lập trình

We are using DbScheduler spring starter. One requirement from the library is to declare the “Tasks beans” at startup (they are injected in the constructor of the autoconfiguration class)

We want/need to register dynamic bean definitions just before this autoconfiguration. To do that we added the @AutoConfigureBefore({DbSchedulerAutoConfiguration.class}) statement on our own configuration and we are using BeanDefinitionRegistryPostProcessor

Now we are facing an issue about the Spring lifecycle. When registering the new Bean we need some data coming from others Beans. We would like to inject the beans implementing OurOwnTaskModel interface. But the beans need Business Service dependencies and Spring fails because there is no no-arg constructor.

To bypass this issue, we replaced the constructor dependencies by a unique ApplicationContext dependency and we retrieve the Business services dependencies later (inside the moethod at runtime). At startup all is good (

Is there a better way delay this dependencies resolution and keep constructor injection ? remove the ApplicationContext injection which is now spread into various classes.

InitializingBean is not appropriate for this stuff, read about FactoryBean but didn’t try yet. Do you know a Spring pattern to achieve this please ?

I read How exactly does the Spring BeanPostProcessor work? . Do I need to try a BeanPostProcessor to wire my dependencies in place of ApplicationContext ? Can I do that ?

LEAVE A COMMENT