question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Referencing a @Bean method in a @Configuration class' @PostConstruct method leads to circular reference

See original GitHub issue

After upgrading to spring-boot 2.6.x, it is no longer possible to access a bean in the @PostConstruct method of the @Configuration class where it was defined. This did work before (tested with 2.1.x, 2.5.6).

Example code that will result in a BeanCurrentlyInCreationException, printing “Application Failed to start”, with “The dependencies of some of the beans in the application context form a cycle:”

@Configuration
public class AppConf {

    @Bean
    public String getTestBean() {
        return "";
    }

    @PostConstruct
    public void init() {
        getTestBean();
    }

}

You can get a full example at https://github.com/BartRobeyns/boot26ConfigPostConstruct (spring-initializer project with only the @Configuration class above added) that fails to build.

Changing the spring-boot version to 2.5.x will allow the application to build and run.

Note that I’m totally fine with this behavior (@PostConstruct in a @Configuration class seems pretty weird to me), but the documentation of @PostConstruct does state that the

The PostConstruct annotation is used on a method that needs to be executed after dependency injection is done to perform any initialization. This annotation must be supported on all classes that support dependency injection.

… so it’s easy to expect the sample above to work. An explicit mention that you shouldn’t access @Bean methods from the @Configuration class itself would clear that up.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
BartRobeynscommented, Feb 9, 2022

@snicoll that clarifies the release notes for me, thanks. I still think that an extra warning in the documentation that you shouldn’t access @Bean methods from the @Configuration class itself would help users that are puzzled by the circular reference error mentioning only the @Configuration class; it’s difficult for a user to understand how that @Configuration class can have an invalid circular reference to itself. (But feel free to close this ticket without further action if you disagree)

1reaction
rstoyanchevcommented, Jan 4, 2022

Added to triage queue as there is some change in behavior, possibly requiring a small documentation update/clarification?

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - What is the expected behaviour of @PostConstruct in ...
If dependencies are supplied via setter or field injection the circular dependency is not reported, and instead incomplete beans are used.
Read more >
Circular Dependencies in Spring - Baeldung
But with a circular dependency, Spring cannot decide which of the beans should be created first since they depend on one another.
Read more >
How to resolve circular dependencies in Spring? - LogicBig
Circular dependencies are the scenarios when two or more beans try to inject each other via constructor.
Read more >
Spring Framework Reference Documentation
Using the @Import annotation; Conditionally include @Configuration classes or @Bean methods; Combining Java and XML configuration.
Read more >
Circular dependency in Spring - Pradeesh Bm - Medium
@PostConstruct on a method will be invoked once the bean is instantiated. Using @Autowired on one of the beans and use a init...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found