Thomas Vahrst opened SPR-15843 and commented

We experienced a BeanCurrentlyInCreationException in a Spring-Boot application when we introduced a BeanPostProcessor to wrap the current datasource. The problem was, that this exception occured randomly, so we tried to debug and reduce the problem. In the end we build a simple Spring testcase to demonstrate, that the order of @Bean definitions in a @Configuration class leads to different behavior, one time passing, one time failing with a BeanCurrentlyInCreationException.

Failing:

    @Bean
    @DependsOn("datasource")  
    public TxManager txmanager(){
        return new TxManager();
    }

    @Bean
    public Datasource datasource(){
        return new Datasource();
    }

... so more beans...

Passing:


    @Bean
    public Datasource datasource(){
        return new Datasource();
    }

    @Bean
    @DependsOn("datasource")  
    public TxManager txmanager(){
        return new TxManager();
    }

... some more beans

I put the complete testcase in github: https://github.com/tvahrst/spring_dependson_problem In the original spring-boot application, it was not the order of @Bean methods in the @Configuration, but different @Autowird fields, which led to different order of bean creation.

It seems, that this 'flaky' behavior is due to the fact, that some 'dependsOn' metainformation for beans are analyzed during bean creation. The dependenciesForBeanMap in DefaultSingletonRegistry is filled during preInstantiateSingletons.


Affects: 4.3.10

Comment From: snicoll

@tvahrst sorry for taking so long. The sample above no longer exists, would you be able to share it again?

Comment From: tvahrst

ui, I lost the sample code. I tried to recover the issue but with my informations provided in this issue I could not reconstruct a failing example code... So I would suggest to close the issue.

Comment From: sbrannen

Thanks for the feedback, @tvahrst.

We'll close this issue as a result.