Skip to content

Commit 9376e63

Browse files
committed
Revise IoC container introduction for modern configuration styles
Includes @configuration(proxyBeanMethods=false) documentation. Closes gh-32429
1 parent 0eb937a commit 9376e63

File tree

5 files changed

+80
-108
lines changed

5 files changed

+80
-108
lines changed

framework-docs/modules/ROOT/pages/core/beans/annotation-config.adoc

+17-31
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
11
[[beans-annotation-config]]
22
= Annotation-based Container Configuration
33

4-
.Are annotations better than XML for configuring Spring?
5-
****
6-
The introduction of annotation-based configuration raised the question of whether this
7-
approach is "`better`" than XML. The short answer is "`it depends.`" The long answer is
8-
that each approach has its pros and cons, and, usually, it is up to the developer to
9-
decide which strategy suits them better. Due to the way they are defined, annotations
10-
provide a lot of context in their declaration, leading to shorter and more concise
11-
configuration. However, XML excels at wiring up components without touching their source
12-
code or recompiling them. Some developers prefer having the wiring close to the source
13-
while others argue that annotated classes are no longer POJOs and, furthermore, that the
14-
configuration becomes decentralized and harder to control.
4+
Spring provides comprehensive support for annotation-based configuration, operating on
5+
metadata in the component class itself by using annotations on the relevant class,
6+
method, or field declaration. As mentioned in
7+
xref:core/beans/factory-extension.adoc#beans-factory-extension-bpp-examples-aabpp[Example: The `AutowiredAnnotationBeanPostProcessor`],
8+
Spring uses `BeanPostProcessors` in conjunction with annotations to make the core IOC
9+
container aware of specific annotations.
1510

16-
No matter the choice, Spring can accommodate both styles and even mix them together.
17-
It is worth pointing out that through its xref:core/beans/java.adoc[JavaConfig] option, Spring lets
18-
annotations be used in a non-invasive way, without touching the target components'
19-
source code and that, in terms of tooling, all configuration styles are supported by
20-
{spring-site-tools}[Spring Tools] for Eclipse, Visual Studio Code, and Theia.
21-
****
22-
23-
An alternative to XML setup is provided by annotation-based configuration, which relies
24-
on bytecode metadata for wiring up components instead of XML declarations. Instead of
25-
using XML to describe a bean wiring, the developer moves the configuration into the
26-
component class itself by using annotations on the relevant class, method, or field
27-
declaration. As mentioned in xref:core/beans/factory-extension.adoc#beans-factory-extension-bpp-examples-aabpp[Example: The `AutowiredAnnotationBeanPostProcessor`], using a
28-
`BeanPostProcessor` in conjunction with annotations is a common means of extending the
29-
Spring IoC container. For example, the xref:core/beans/annotation-config/autowired.adoc[`@Autowired`]
30-
annotation provides the same capabilities as described in xref:core/beans/dependencies/factory-autowire.adoc[Autowiring Collaborators] but
11+
For example, the xref:core/beans/annotation-config/autowired.adoc[`@Autowired`]
12+
annotation provides the same capabilities as described in
13+
xref:core/beans/dependencies/factory-autowire.adoc[Autowiring Collaborators] but
3114
with more fine-grained control and wider applicability. In addition, Spring provides
3215
support for JSR-250 annotations, such as `@PostConstruct` and `@PreDestroy`, as well as
3316
support for JSR-330 (Dependency Injection for Java) annotations contained in the
@@ -36,13 +19,16 @@ can be found in the xref:core/beans/standard-annotations.adoc[relevant section].
3619

3720
[NOTE]
3821
====
39-
Annotation injection is performed before XML injection. Thus, the XML configuration
40-
overrides the annotations for properties wired through both approaches.
22+
Annotation injection is performed before external property injection. Thus, external
23+
configuration (e.g. XML-specified bean properties) effectively overrides the annotations
24+
for properties when wired through mixed approaches.
4125
====
4226

43-
As always, you can register the post-processors as individual bean definitions, but they
44-
can also be implicitly registered by including the following tag in an XML-based Spring
45-
configuration (notice the inclusion of the `context` namespace):
27+
Technically, you can register the post-processors as individual bean definitions, but they
28+
are implicitly registered in an `AnnotationConfigApplicationContext` already.
29+
30+
In an XML-based Spring setup, you may include the following configuration tag to enable
31+
mixing and matching with annotation-based configuration:
4632

4733
[source,xml,indent=0,subs="verbatim,quotes"]
4834
----

framework-docs/modules/ROOT/pages/core/beans/basics.adoc

+38-53
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,28 @@
22
= Container Overview
33

44
The `org.springframework.context.ApplicationContext` interface represents the Spring IoC
5-
container and is responsible for instantiating, configuring, and assembling the
6-
beans. The container gets its instructions on what objects to
7-
instantiate, configure, and assemble by reading configuration metadata. The
8-
configuration metadata is represented in XML, Java annotations, or Java code. It lets
9-
you express the objects that compose your application and the rich interdependencies
10-
between those objects.
11-
12-
Several implementations of the `ApplicationContext` interface are supplied
13-
with Spring. In stand-alone applications, it is common to create an
14-
instance of
15-
{spring-framework-api}/context/support/ClassPathXmlApplicationContext.html[`ClassPathXmlApplicationContext`]
16-
or {spring-framework-api}/context/support/FileSystemXmlApplicationContext.html[`FileSystemXmlApplicationContext`].
17-
While XML has been the traditional format for defining configuration metadata, you can
18-
instruct the container to use Java annotations or code as the metadata format by
19-
providing a small amount of XML configuration to declaratively enable support for these
20-
additional metadata formats.
5+
container and is responsible for instantiating, configuring, and assembling the beans.
6+
The container gets its instructions on the components to instantiate, configure, and
7+
assemble by reading configuration metadata. The configuration metadata can be represented
8+
as annotated component classes, configuration classes with factory methods, or external
9+
XML files or Groovy scripts. With either format, you may compose your application and the
10+
rich interdependencies between those components.
11+
12+
Several implementations of the `ApplicationContext` interface are part of core Spring.
13+
In stand-alone applications, it is common to create an instance of
14+
{spring-framework-api}/context/annotation/AnnotationConfigApplicationContext.html[`AnnotationConfigApplicationContext`]
15+
or {spring-framework-api}/context/support/ClassPathXmlApplicationContext.html[`ClassPathXmlApplicationContext`].
2116

2217
In most application scenarios, explicit user code is not required to instantiate one or
23-
more instances of a Spring IoC container. For example, in a web application scenario, a
24-
simple eight (or so) lines of boilerplate web descriptor XML in the `web.xml` file
25-
of the application typically suffices (see
18+
more instances of a Spring IoC container. For example, in a plain web application scenario,
19+
a simple boilerplate web descriptor XML in the `web.xml` file of the application suffices (see
2620
xref:core/beans/context-introduction.adoc#context-create[Convenient ApplicationContext Instantiation for Web Applications]).
27-
If you use the {spring-site-tools}[Spring Tools for Eclipse] (an Eclipse-powered
28-
development environment), you can easily create this boilerplate configuration with a
29-
few mouse clicks or keystrokes.
21+
In a Spring Boot scenario, the application context is implicitly bootstrapped for you
22+
based on common setup conventions.
3023

3124
The following diagram shows a high-level view of how Spring works. Your application classes
3225
are combined with configuration metadata so that, after the `ApplicationContext` is
33-
created and initialized, you have a fully configured and executable system or
34-
application.
26+
created and initialized, you have a fully configured and executable system or application.
3527

3628
.The Spring IoC container
3729
image::container-magic.png[]
@@ -43,33 +35,25 @@ image::container-magic.png[]
4335

4436
As the preceding diagram shows, the Spring IoC container consumes a form of
4537
configuration metadata. This configuration metadata represents how you, as an
46-
application developer, tell the Spring container to instantiate, configure, and assemble
47-
the objects in your application.
38+
application developer, tell the Spring container to instantiate, configure,
39+
and assemble the components in your application.
4840

49-
Configuration metadata is traditionally supplied in a simple and intuitive XML format,
50-
which is what most of this chapter uses to convey key concepts and features of the
51-
Spring IoC container.
52-
53-
NOTE: XML-based metadata is not the only allowed form of configuration metadata.
5441
The Spring IoC container itself is totally decoupled from the format in which this
5542
configuration metadata is actually written. These days, many developers choose
56-
xref:core/beans/java.adoc[Java-based configuration] for their Spring applications.
57-
58-
For information about using other forms of metadata with the Spring container, see:
43+
xref:core/beans/java.adoc[Java-based configuration] for their Spring applications:
5944

6045
* xref:core/beans/annotation-config.adoc[Annotation-based configuration]: define beans using
61-
annotation-based configuration metadata.
46+
annotation-based configuration metadata on your application's component classes.
6247
* xref:core/beans/java.adoc[Java-based configuration]: define beans external to your application
63-
classes by using Java rather than XML files. To use these features, see the
48+
classes by using Java-based configuration classes. To use these features, see the
6449
{spring-framework-api}/context/annotation/Configuration.html[`@Configuration`],
6550
{spring-framework-api}/context/annotation/Bean.html[`@Bean`],
6651
{spring-framework-api}/context/annotation/Import.html[`@Import`],
6752
and {spring-framework-api}/context/annotation/DependsOn.html[`@DependsOn`] annotations.
6853

69-
Spring configuration consists of at least one and typically more than one bean
70-
definition that the container must manage. XML-based configuration metadata configures these
71-
beans as `<bean/>` elements inside a top-level `<beans/>` element. Java
72-
configuration typically uses `@Bean`-annotated methods within a `@Configuration` class.
54+
Spring configuration consists of at least one and typically more than one bean definition
55+
that the container must manage. Java configuration typically uses `@Bean`-annotated
56+
methods within a `@Configuration` class, each corresponding to one bean definition.
7357

7458
These bean definitions correspond to the actual objects that make up your application.
7559
Typically, you define service layer objects, persistence layer objects such as
@@ -79,7 +63,14 @@ Typically, one does not configure fine-grained domain objects in the container,
7963
it is usually the responsibility of repositories and business logic to create and load
8064
domain objects.
8165

82-
The following example shows the basic structure of XML-based configuration metadata:
66+
67+
68+
[[beans-factory-xml]]
69+
=== XML as an External Configuration DSL
70+
71+
XML-based configuration metadata configures these beans as `<bean/>` elements inside
72+
a top-level `<beans/>` element. The following example shows the basic structure of
73+
XML-based configuration metadata:
8374

8475
[source,xml,indent=0,subs="verbatim,quotes"]
8576
----
@@ -110,14 +101,9 @@ The value of the `id` attribute can be used to refer to collaborating objects. T
110101
for referring to collaborating objects is not shown in this example. See
111102
xref:core/beans/dependencies.adoc[Dependencies] for more information.
112103

113-
114-
115-
[[beans-factory-instantiation]]
116-
== Instantiating a Container
117-
118-
The location path or paths
119-
supplied to an `ApplicationContext` constructor are resource strings that let
120-
the container load configuration metadata from a variety of external resources, such
104+
For instantiating a container, the location path or paths to the XML resource files
105+
need to be supplied to a `ClassPathXmlApplicationContext` constructor that let the
106+
container load configuration metadata from a variety of external resources, such
121107
as the local file system, the Java `CLASSPATH`, and so on.
122108

123109
[tabs]
@@ -209,9 +195,9 @@ xref:core/beans/dependencies.adoc[Dependencies].
209195
It can be useful to have bean definitions span multiple XML files. Often, each individual
210196
XML configuration file represents a logical layer or module in your architecture.
211197

212-
You can use the application context constructor to load bean definitions from all these
198+
You can use the `ClassPathXmlApplicationContext` constructor to load bean definitions from
213199
XML fragments. This constructor takes multiple `Resource` locations, as was shown in the
214-
xref:core/beans/basics.adoc#beans-factory-instantiation[previous section]. Alternatively,
200+
xref:core/beans/basics.adoc#beans-factory-xml[previous section]. Alternatively,
215201
use one or more occurrences of the `<import/>` element to load bean definitions from
216202
another file or files. The following example shows how to do so:
217203

@@ -259,7 +245,7 @@ configuration features beyond plain bean definitions are available in a selectio
259245
of XML namespaces provided by Spring -- for example, the `context` and `util` namespaces.
260246

261247

262-
[[groovy-bean-definition-dsl]]
248+
[[beans-factory-groovy]]
263249
=== The Groovy Bean Definition DSL
264250

265251
As a further example for externalized configuration metadata, bean definitions can also
@@ -420,4 +406,3 @@ a dependency on a specific bean through metadata (such as an autowiring annotati
420406

421407

422408

423-

framework-docs/modules/ROOT/pages/core/beans/java/basic-concepts.adoc

+23-22
Original file line numberDiff line numberDiff line change
@@ -55,33 +55,34 @@ The preceding `AppConfig` class is equivalent to the following Spring `<beans/>`
5555
</beans>
5656
----
5757

58-
.Full @Configuration vs "`lite`" @Bean mode?
58+
.@Configuration classes with or without local calls between @Bean methods?
5959
****
60-
When `@Bean` methods are declared within classes that are not annotated with
61-
`@Configuration`, they are referred to as being processed in a "`lite`" mode. Bean methods
62-
declared on a bean that is not annotated with `@Configuration` are considered to be "`lite`",
63-
with a different primary purpose of the containing class and a `@Bean` method
64-
being a sort of bonus there. For example, service components may expose management views
65-
to the container through an additional `@Bean` method on each applicable component class.
66-
In such scenarios, `@Bean` methods are a general-purpose factory method mechanism.
67-
68-
Unlike full `@Configuration`, lite `@Bean` methods cannot declare inter-bean dependencies.
69-
Instead, they operate on their containing component's internal state and, optionally, on
70-
arguments that they may declare. Such a `@Bean` method should therefore not invoke other
71-
`@Bean` methods. Each such method is literally only a factory method for a particular
72-
bean reference, without any special runtime semantics. The positive side-effect here is
73-
that no CGLIB subclassing has to be applied at runtime, so there are no limitations in
74-
terms of class design (that is, the containing class may be `final` and so forth).
75-
7660
In common scenarios, `@Bean` methods are to be declared within `@Configuration` classes,
77-
ensuring that "`full`" mode is always used and that cross-method references therefore
78-
get redirected to the container's lifecycle management. This prevents the same
79-
`@Bean` method from accidentally being invoked through a regular Java call, which helps
80-
to reduce subtle bugs that can be hard to track down when operating in "`lite`" mode.
61+
ensuring that full configuration class processing applies and that cross-method
62+
references therefore get redirected to the container's lifecycle management.
63+
This prevents the same `@Bean` method from accidentally being invoked through a regular
64+
Java method call, which helps to reduce subtle bugs that can be hard to track down.
65+
66+
When `@Bean` methods are declared within classes that are not annotated with
67+
`@Configuration` - or when `@Configuration(proxyBeanMethods=false)` is declared -,
68+
they are referred to as being processed in a "lite" mode. In such scenarios,
69+
`@Bean` methods are effectively a general-purpose factory method mechanism without
70+
special runtime processing (that is, without generating a CGLIB subclass for it).
71+
A custom Java call to such a method will not get intercepted by the container and
72+
therefore behaves just like a regular method call, creating a new instance every time
73+
rather than reusing an existing singleton (or scoped) instance for the given bean.
74+
75+
As a consequence, `@Bean` methods on classes without runtime proxying are not meant to
76+
declare inter-bean dependencies at all. Instead, they are expected to operate on their
77+
containing component's fields and, optionally, on arguments that a factory method may
78+
declare in order to receive autowired collaborators. Such a `@Bean` method therefore
79+
never needs to invoke other `@Bean` methods; every such call can be expressed through
80+
a factory method argument instead. The positive side-effect here is that no CGLIB
81+
subclassing has to be applied at runtime, reducing the overhead and the footprint.
8182
****
8283

8384
The `@Bean` and `@Configuration` annotations are discussed in depth in the following sections.
84-
First, however, we cover the various ways of creating a spring container by using
85+
First, however, we cover the various ways of creating a Spring container by using
8586
Java-based configuration.
8687

8788

framework-docs/modules/ROOT/pages/languages/groovy.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ existing Java application.
88

99
The Spring Framework provides a dedicated `ApplicationContext` that supports a Groovy-based
1010
Bean Definition DSL. For more details, see
11-
xref:core/beans/basics.adoc#groovy-bean-definition-dsl[The Groovy Bean Definition DSL].
11+
xref:core/beans/basics.adoc#beans-factory-groovy[The Groovy Bean Definition DSL].
1212

1313
Further support for Groovy, including beans written in Groovy, refreshable script beans,
1414
and more is available in xref:languages/dynamic.adoc[Dynamic Language Support].

framework-docs/modules/ROOT/pages/testing/testcontext-framework/ctx-management/groovy.adoc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
= Context Configuration with Groovy Scripts
33

44
To load an `ApplicationContext` for your tests by using Groovy scripts that use the
5-
xref:core/beans/basics.adoc#groovy-bean-definition-dsl[Groovy Bean Definition DSL], you can annotate
5+
xref:core/beans/basics.adoc#beans-factory-groovy[Groovy Bean Definition DSL], you can annotate
66
your test class with `@ContextConfiguration` and configure the `locations` or `value`
77
attribute with an array that contains the resource locations of Groovy scripts. Resource
88
lookup semantics for Groovy scripts are the same as those described for

0 commit comments

Comments
 (0)