@@ -1168,7 +1168,7 @@ load an entire Spring IoC container instance.
1168
1168
****
1169
1169
Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to
1170
1170
use constructors for mandatory dependencies and setter methods or configuration methods
1171
- for optional dependencies. Note that use of the <<beans-required -annotation, @Required >>
1171
+ for optional dependencies. Note that use of the <<beans-autowired -annotation, @Autowired >>
1172
1172
annotation on a setter method can be used to make the property be a required dependency;
1173
1173
however, constructor injection with programmatic validation of arguments is preferable.
1174
1174
@@ -4600,22 +4600,21 @@ source code and that, in terms of tooling, all configuration styles are supporte
4600
4600
https://spring.io/tools[Spring Tools for Eclipse].
4601
4601
****
4602
4602
4603
- An alternative to XML setup is provided by annotation-based configuration, which relies on
4604
- the bytecode metadata for wiring up components instead of angle-bracket declarations.
4603
+ An alternative to XML setup is provided by annotation-based configuration, which relies
4604
+ on the bytecode metadata for wiring up components instead of angle-bracket declarations.
4605
4605
Instead of using XML to describe a bean wiring, the developer moves the configuration
4606
4606
into the component class itself by using annotations on the relevant class, method, or
4607
4607
field declaration. As mentioned in <<beans-factory-extension-bpp-examples-aabpp>>, using
4608
4608
a `BeanPostProcessor` in conjunction with annotations is a common means of extending the
4609
- Spring IoC container. For example, Spring 2.0 introduced the possibility of enforcing
4610
- required properties with the <<beans-required-annotation,`@Required`>> annotation. Spring
4611
- 2.5 made it possible to follow that same general approach to drive Spring's dependency
4612
- injection. Essentially, the `@Autowired` annotation provides the same capabilities as
4613
- described in <<beans-factory-autowire>> but with more fine-grained control and wider
4614
- applicability. Spring 2.5 also added support for JSR-250 annotations, such as
4615
- `@PostConstruct` and `@PreDestroy`. Spring 3.0 added support for JSR-330 (Dependency
4616
- Injection for Java) annotations contained in the `jakarta.inject` package such as
4617
- `@Inject` and `@Named`. Details about those annotations can be found in the
4618
- <<beans-standard-annotations,relevant section>>.
4609
+ Spring IoC container. For example, Spring 2.5 introduced an annotation-based approach to
4610
+ drive Spring's dependency injection. Essentially, the <<beans-autowired-annotation,
4611
+ `@Autowired`>> annotation provides the same capabilities as described in
4612
+ <<beans-factory-autowire>> but with more fine-grained control and wider applicability.
4613
+ Spring 2.5 also added support for JSR-250 annotations, such as `@PostConstruct` and
4614
+ `@PreDestroy`. Spring 3.0 added support for JSR-330 (Dependency Injection for Java)
4615
+ annotations contained in the `jakarta.inject` package such as `@Inject` and `@Named`.
4616
+ Details about those annotations can be found in the <<beans-standard-annotations,
4617
+ relevant section>>.
4619
4618
4620
4619
[NOTE]
4621
4620
====
@@ -4662,64 +4661,6 @@ it only checks for `@Autowired` beans in your controllers, and not your services
4662
4661
4663
4662
4664
4663
4665
- [[beans-required-annotation]]
4666
- === @Required
4667
-
4668
- The `@Required` annotation applies to bean property setter methods, as in the following
4669
- example:
4670
-
4671
- [source,java,indent=0,subs="verbatim,quotes",role="primary"]
4672
- .Java
4673
- ----
4674
- public class SimpleMovieLister {
4675
-
4676
- private MovieFinder movieFinder;
4677
-
4678
- @Required
4679
- public void setMovieFinder(MovieFinder movieFinder) {
4680
- this.movieFinder = movieFinder;
4681
- }
4682
-
4683
- // ...
4684
- }
4685
- ----
4686
- [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
4687
- .Kotlin
4688
- ----
4689
- class SimpleMovieLister {
4690
-
4691
- lateinit var movieFinder: MovieFinder
4692
- @Required
4693
- set
4694
-
4695
- // ...
4696
- }
4697
- ----
4698
-
4699
- This annotation indicates that the affected bean property must be populated at
4700
- configuration time, through an explicit property value in a bean definition or through
4701
- autowiring. The container throws an exception if the affected bean property has not been
4702
- populated. This allows for eager and explicit failure, avoiding `NullPointerException`
4703
- instances or the like later on. We still recommend that you put assertions into the
4704
- bean class itself (for example, into an init method). Doing so enforces those required
4705
- references and values even when you use the class outside of a container.
4706
-
4707
- [TIP]
4708
- ====
4709
- The {api-spring-framework}/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.html[`RequiredAnnotationBeanPostProcessor`]
4710
- must be registered as a bean to enable support for the `@Required` annotation.
4711
- ====
4712
-
4713
- [NOTE]
4714
- ====
4715
- The `@Required` annotation and `RequiredAnnotationBeanPostProcessor` are formally
4716
- deprecated as of Spring Framework 5.1, in favor of using constructor injection for
4717
- required settings (or a custom implementation of `InitializingBean.afterPropertiesSet()`
4718
- or a custom `@PostConstruct` method along with bean property setter methods).
4719
- ====
4720
-
4721
-
4722
-
4723
4664
[[beans-autowired-annotation]]
4724
4665
=== Using `@Autowired`
4725
4666
@@ -5026,10 +4967,19 @@ non-required (i.e., by setting the `required` attribute in `@Autowired` to `fals
5026
4967
}
5027
4968
----
5028
4969
4970
+ [NOTE]
4971
+ ====
5029
4972
A non-required method will not be called at all if its dependency (or one of its
5030
4973
dependencies, in case of multiple arguments) is not available. A non-required field will
5031
4974
not get populated at all in such cases, leaving its default value in place.
5032
4975
4976
+ In other words, setting the `required` attribute to `false` indicates that the
4977
+ corresponding property is _optional_ for autowiring purposes, and the property will be
4978
+ ignored if it cannot be autowired. This allows properties to be assigned default values
4979
+ that can be optionally overridden via dependency injection.
4980
+ ====
4981
+
4982
+
5033
4983
[[beans-autowired-annotation-constructor-resolution]]
5034
4984
5035
4985
Injected constructor and factory method arguments are a special case since the `required`
@@ -5057,13 +5007,6 @@ declares multiple constructors but none of them is annotated with `@Autowired`,
5057
5007
primary/default constructor (if present) will be used. If a class only declares a single
5058
5008
constructor to begin with, it will always be used, even if not annotated. Note that an
5059
5009
annotated constructor does not have to be public.
5060
-
5061
- The `required` attribute of `@Autowired` is recommended over the deprecated `@Required`
5062
- annotation on setter methods. Setting the `required` attribute to `false` indicates that
5063
- the property is not required for autowiring purposes, and the property is ignored if it
5064
- cannot be autowired. `@Required`, on the other hand, is stronger in that it enforces the
5065
- property to be set by any means supported by the container, and if no value is defined,
5066
- a corresponding exception is raised.
5067
5010
====
5068
5011
5069
5012
Alternatively, you can express the non-required nature of a particular dependency
@@ -7538,10 +7481,6 @@ features are not available, as the following table shows:
7538
7481
| -
7539
7482
| no equivalent
7540
7483
7541
- | @Required
7542
- | -
7543
- | no equivalent
7544
-
7545
7484
| @Lazy
7546
7485
| -
7547
7486
| no equivalent
0 commit comments