From 3849ed3a0c6da57db601d66d352a80b34006bbe0 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Fri, 29 Sep 2023 15:38:55 +0200 Subject: [PATCH] Explicit note on local bean access within @PostConstruct method Closes gh-27876 --- src/docs/asciidoc/core/core-beans.adoc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/docs/asciidoc/core/core-beans.adoc b/src/docs/asciidoc/core/core-beans.adoc index 2ae891afdc..edc3b9bd53 100644 --- a/src/docs/asciidoc/core/core-beans.adoc +++ b/src/docs/asciidoc/core/core-beans.adoc @@ -8883,7 +8883,6 @@ classes, each depending on beans declared in the others: } ---- - There is another way to achieve the same result. Remember that `@Configuration` classes are ultimately only another bean in the container: This means that they can take advantage of `@Autowired` and `@Value` injection and other features the same as any other bean. @@ -8895,6 +8894,11 @@ classes are processed quite early during the initialization of the context, and to be injected this way may lead to unexpected early initialization. Whenever possible, resort to parameter-based injection, as in the preceding example. +Avoid access to locally defined beans within a `@PostConstruct` method on the same configuration +class. This effectively leads to a circular reference since non-static `@Bean` methods semantically +require a fully initialized configuration class instance to be called on. With circular references +disallowed (e.g. in Spring Boot 2.6+), this may trigger a `BeanCurrentlyInCreationException`. + Also, be particularly careful with `BeanPostProcessor` and `BeanFactoryPostProcessor` definitions through `@Bean`. Those should usually be declared as `static @Bean` methods, not triggering the instantiation of their containing configuration class. Otherwise, `@Autowired` and `@Value` may not