Skip to content

Commit 4cf5c77

Browse files
committed
Explicit note on local bean access within @PostConstruct method
Closes gh-27876
1 parent 847e8a2 commit 4cf5c77

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

framework-docs/modules/ROOT/pages/core/beans/java/composing-configuration-classes.adoc

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ issue, because no compiler is involved, and you can declare
113113
When using `@Configuration` classes, the Java compiler places constraints on
114114
the configuration model, in that references to other beans must be valid Java syntax.
115115

116-
Fortunately, solving this problem is simple. As xref:core/beans/java/bean-annotation.adoc#beans-java-dependencies[we already discussed],
116+
Fortunately, solving this problem is simple. As
117+
xref:core/beans/java/bean-annotation.adoc#beans-java-dependencies[we already discussed],
117118
a `@Bean` method can have an arbitrary number of parameters that describe the bean
118119
dependencies. Consider the following more real-world scenario with several `@Configuration`
119120
classes, each depending on beans declared in the others:
@@ -204,7 +205,6 @@ Kotlin::
204205
----
205206
======
206207

207-
208208
There is another way to achieve the same result. Remember that `@Configuration` classes are
209209
ultimately only another bean in the container: This means that they can take advantage of
210210
`@Autowired` and `@Value` injection and other features the same as any other bean.
@@ -216,6 +216,11 @@ classes are processed quite early during the initialization of the context, and
216216
to be injected this way may lead to unexpected early initialization. Whenever possible, resort to
217217
parameter-based injection, as in the preceding example.
218218
219+
Avoid access to locally defined beans within a `@PostConstruct` method on the same configuration
220+
class. This effectively leads to a circular reference since non-static `@Bean` methods semantically
221+
require a fully initialized configuration class instance to be called on. With circular references
222+
disallowed (e.g. in Spring Boot 2.6+), this may trigger a `BeanCurrentlyInCreationException`.
223+
219224
Also, be particularly careful with `BeanPostProcessor` and `BeanFactoryPostProcessor` definitions
220225
through `@Bean`. Those should usually be declared as `static @Bean` methods, not triggering the
221226
instantiation of their containing configuration class. Otherwise, `@Autowired` and `@Value` may not

0 commit comments

Comments
 (0)