Skip to content

Commit be9c096

Browse files
committed
Extended coverage of full vs lite mode for configuration classes
Issue: SPR-16076 (cherry picked from commit 17fb4fe)
1 parent d1b5b5d commit be9c096

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/docs/asciidoc/core/core-beans.adoc

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4517,6 +4517,7 @@ any). These types must be 'wired up' explicitly via XML or using a Spring `@Bean
45174517
====
45184518

45194519

4520+
45204521
[[beans-autowired-annotation-primary]]
45214522
=== Fine-tuning annotation-based autowiring with @Primary
45224523

@@ -5010,7 +5011,6 @@ Generic qualifiers also apply when autowiring Lists, Maps and Arrays:
50105011

50115012

50125013

5013-
50145014
[[beans-custom-autowire-configurer]]
50155015
=== CustomAutowireConfigurer
50165016

@@ -6168,27 +6168,37 @@ The `AppConfig` class above would be equivalent to the following Spring `<beans/
61686168
</beans>
61696169
----
61706170

6171-
.Full @Configuration vs 'lite' @Beans mode?
6171+
.Full @Configuration vs 'lite' @Bean mode?
61726172
****
61736173
When `@Bean` methods are declared within classes that are __not__ annotated with
6174-
`@Configuration` they are referred to as being processed in a 'lite' mode. For example,
6175-
bean methods declared in a `@Component` or even in a __plain old class__ will be
6176-
considered 'lite'.
6177-
6178-
Unlike full `@Configuration`, lite `@Bean` methods cannot easily declare inter-bean
6179-
dependencies. Usually one `@Bean` method should not invoke another `@Bean` method when
6180-
operating in 'lite' mode.
6181-
6182-
Only using `@Bean` methods within `@Configuration` classes is a recommended approach of
6183-
ensuring that 'full' mode is always used. This will prevent the same `@Bean` method from
6184-
accidentally being invoked multiple times and helps to reduce subtle bugs that can be
6185-
hard to track down when operating in 'lite' mode.
6174+
`@Configuration` they are referred to as being processed in a 'lite' mode. Bean methods
6175+
declared in a `@Component` or even in a __plain old class__ will be considered 'lite',
6176+
with a different primary purpose of the containing class and an `@Bean` method just
6177+
being a sort of bonus there. For example, service components may expose management views
6178+
to the container through an additional `@Bean` method on each applicable component class.
6179+
In such scenarios, `@Bean` methods are a simple general-purpose factory method mechanism.
6180+
6181+
Unlike full `@Configuration`, lite `@Bean` methods cannot declare inter-bean dependencies.
6182+
Instead, they operate on their containing component's internal state and optionally on
6183+
arguments that they may declare. Such an `@Bean` method should therefore not invoke other
6184+
`@Bean` methods; each such method is literally just a factory method for a particular
6185+
bean reference, without any special runtime semantics. The positive side-effect here is
6186+
that no CGLIB subclassing has to be applied at runtime, so there are no limitations in
6187+
terms of class design (i.e. the containing class may nevertheless be `final` etc).
6188+
6189+
In common scenarios, `@Bean` methods are to be declared within `@Configuration` classes,
6190+
ensuring that 'full' mode is always used and that cross-method references will therefore
6191+
get redirected to the container's lifecycle management. This will prevent the same
6192+
`@Bean` method from accidentally being invoked through a regular Java call which helps
6193+
to reduce subtle bugs that can be hard to track down when operating in 'lite' mode.
61866194
****
61876195

61886196
The `@Bean` and `@Configuration` annotations will be discussed in depth in the sections
61896197
below. First, however, we'll cover the various ways of creating a spring container using
61906198
Java-based configuration.
61916199

6200+
6201+
61926202
[[beans-java-instantiating-container]]
61936203
=== Instantiating the Spring container using AnnotationConfigApplicationContext
61946204

0 commit comments

Comments
 (0)