Affects: spring: 5.3.22 spring-boot: 2.7.3
Under some circumstances using BeanFactory property values causes bean to be initialized in wrong order without applying proper BeanPostProcessor: If there is BeanFactory that returns null by getObjectType() in non-initialized state (according to the contract currently that means type if not known at the time of the call - see org.springframework.beans.factory.FactoryBean#getObjectType) If another bean is used as a value for BeanFactory (that can be jaxws:client - http://cxf.apache.org/jaxws) If there is BeanFactoryPostProcessor that calls beanFactory.findAnnotationOnBean() (that can be JdbcTemplateAutoConfiguration -> DatabaseInitializationDependencyConfigurer$DependsOnDatabaseInitializationPostProcessor) then it causes value bean to be initialized in wrong order without proper BeanPostProcessor applying.
I've created test case for that: https://github.com/mpashka/spring-init-test1/tree/spring-boot-v2.7.3. It shows that BeanPostProcessor is not applied for bean.
Also it can be good idea to specify order in which beans are initialized under all circumstances. With all processors, post processors, factory post processors, ordered, priority ordered, init methods, e.t.c.
Comment From: snicoll
Thanks for the sample. I am afraid this is the expected behavior. The log of your failing test even said so explicitly:
2023-10-02 15:18:52.400 INFO 21690 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'myBeanProcessedChecker' of type [org.mpashka.spring.init_test1.impl.MyBeanProcessedChecker] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2023-10-02 15:18:52.401 INFO 21690 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'myBeanFactory' of type [org.mpashka.spring.init_test1.impl.MyBeanFactory] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
Because you didn't indicate the type of bean your bean factory produces, the context has to initialize that bean early. At that point the BPP you've defined can't be applied. Regardless of this issue, don't expose BeanPostProcessor
using component scanning. Those must be defined early and with the static
keyword if you register them as @Bean
methods.