Skip to content

Commit 90493f4

Browse files
committed
Notes about autowiring in configuration classes and late registration scenarios
Issue: SPR-12970 Issue: SPR-13285
1 parent ff46cec commit 90493f4

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/asciidoc/core-beans.adoc

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,16 @@ supports this registration through the methods `registerSingleton(..)` and
384384
`registerBeanDefinition(..)`. However, typical applications work solely with beans
385385
defined through metadata bean definitions.
386386

387+
[NOTE]
388+
====
389+
Bean metadata and manually supplied singleton instances need to be registered as early
390+
as possible, in order for the container to properly reason about them during autowiring
391+
and other introspection steps. While overriding of existing metadata and existing
392+
singleton instances is supported to some degree, the registration of new beans at
393+
runtime (concurrently with live access to factory) is not officially supported and may
394+
lead to concurrent access exceptions and/or inconsistent state in the bean container.
395+
====
396+
387397

388398

389399
[[beans-beanname]]
@@ -6557,15 +6567,20 @@ classes, each depending on beans declared in the others:
65576567
----
65586568

65596569
There is another way to achieve the same result. Remember that `@Configuration` classes are
6560-
ultimately just another bean in the container - this means that they can take advantage
6561-
of `@Autowired` injection metadata just like any other bean!
6570+
ultimately just another bean in the container: This means that they can take advantage of
6571+
`@Autowired` and `@Value` injection etc just like any other bean!
65626572

65636573
[WARNING]
65646574
====
65656575
Make sure that the dependencies you inject that way are of the simplest kind only. `@Configuration`
6566-
classes are processed quite early during the initialization of the context and forcing a
6567-
dependency to be injected this way may lead to unexpected early initialization. Whenever possible,
6568-
resort to parameter-based injection as in the example above.
6576+
classes are processed quite early during the initialization of the context and forcing a dependency
6577+
to be injected this way may lead to unexpected early initialization. Whenever possible, resort to
6578+
parameter-based injection as in the example above.
6579+
6580+
Also, be particularly careful with `BeanPostProcessor` and `BeanFactoryPostProcessor` definitions
6581+
via `@Bean`. Those should usually be declared as `static @Bean` methods, not triggering the
6582+
instantiation of their containing configuration class. Otherwise, `@Autowired` and `@Value` won't
6583+
work on the configuration class itself since it is being created as a bean instance too early.
65696584
====
65706585

65716586
[source,java,indent=0]

0 commit comments

Comments
 (0)