Skip to content

Commit 17fb4fe

Browse files
committed
Extended coverage of full vs lite mode for configuration classes
Issue: SPR-16076
1 parent 77bab95 commit 17fb4fe

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/asciidoc/core-beans.adoc

+24-14
Original file line numberDiff line numberDiff line change
@@ -4626,6 +4626,7 @@ The corresponding bean definitions appear as follows.
46264626
----
46274627

46284628

4629+
46294630
[[beans-autowired-annotation-qualifiers]]
46304631
=== Fine-tuning annotation-based autowiring with qualifiers
46314632

@@ -5043,7 +5044,6 @@ Generic qualifiers also apply when autowiring Lists, Maps and Arrays:
50435044

50445045

50455046

5046-
50475047
[[beans-custom-autowire-configurer]]
50485048
=== CustomAutowireConfigurer
50495049

@@ -6138,27 +6138,37 @@ The `AppConfig` class above would be equivalent to the following Spring `<beans/
61386138
</beans>
61396139
----
61406140

6141-
.Full @Configuration vs 'lite' @Beans mode?
6141+
.Full @Configuration vs 'lite' @Bean mode?
61426142
****
61436143
When `@Bean` methods are declared within classes that are __not__ annotated with
6144-
`@Configuration` they are referred to as being processed in a 'lite' mode. For example,
6145-
bean methods declared in a `@Component` or even in a __plain old class__ will be
6146-
considered 'lite'.
6147-
6148-
Unlike full `@Configuration`, lite `@Bean` methods cannot easily declare inter-bean
6149-
dependencies. Usually one `@Bean` method should not invoke another `@Bean` method when
6150-
operating in 'lite' mode.
6151-
6152-
Only using `@Bean` methods within `@Configuration` classes is a recommended approach of
6153-
ensuring that 'full' mode is always used. This will prevent the same `@Bean` method from
6154-
accidentally being invoked multiple times and helps to reduce subtle bugs that can be
6155-
hard to track down when operating in 'lite' mode.
6144+
`@Configuration` they are referred to as being processed in a 'lite' mode. Bean methods
6145+
declared in a `@Component` or even in a __plain old class__ will be considered 'lite',
6146+
with a different primary purpose of the containing class and an `@Bean` method just
6147+
being a sort of bonus there. For example, service components may expose management views
6148+
to the container through an additional `@Bean` method on each applicable component class.
6149+
In such scenarios, `@Bean` methods are a simple general-purpose factory method mechanism.
6150+
6151+
Unlike full `@Configuration`, lite `@Bean` methods cannot declare inter-bean dependencies.
6152+
Instead, they operate on their containing component's internal state and optionally on
6153+
arguments that they may declare. Such an `@Bean` method should therefore not invoke other
6154+
`@Bean` methods; each such method is literally just a factory method for a particular
6155+
bean reference, without any special runtime semantics. The positive side-effect here is
6156+
that no CGLIB subclassing has to be applied at runtime, so there are no limitations in
6157+
terms of class design (i.e. the containing class may nevertheless be `final` etc).
6158+
6159+
In common scenarios, `@Bean` methods are to be declared within `@Configuration` classes,
6160+
ensuring that 'full' mode is always used and that cross-method references will therefore
6161+
get redirected to the container's lifecycle management. This will prevent the same
6162+
`@Bean` method from accidentally being invoked through a regular Java call which helps
6163+
to reduce subtle bugs that can be hard to track down when operating in 'lite' mode.
61566164
****
61576165

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

6170+
6171+
61626172
[[beans-java-instantiating-container]]
61636173
=== Instantiating the Spring container using AnnotationConfigApplicationContext
61646174

0 commit comments

Comments
 (0)