You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: framework-docs/modules/ROOT/pages/core/beans/annotation-config.adoc
+17-31
Original file line number
Diff line number
Diff line change
@@ -1,33 +1,16 @@
1
1
[[beans-annotation-config]]
2
2
= Annotation-based Container Configuration
3
3
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.
15
10
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
31
14
with more fine-grained control and wider applicability. In addition, Spring provides
32
15
support for JSR-250 annotations, such as `@PostConstruct` and `@PreDestroy`, as well as
33
16
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].
36
19
37
20
[NOTE]
38
21
====
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.
41
25
====
42
26
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:
0 commit comments