Skip to content

Commit

Permalink
Explicit note on local bean access within @PostConstruct method
Browse files Browse the repository at this point in the history
Closes gh-27876
  • Loading branch information
jhoeller committed Sep 29, 2023
1 parent d62f178 commit 3849ed3
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/docs/asciidoc/core/core-beans.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down

0 comments on commit 3849ed3

Please sign in to comment.