Skip to content

Commit 74d1be9

Browse files
committed
Remove obsolete references to @required
Since the @required annotation was removed in a previous 6.0 milestone, this commit removes all remaining references to it. Closes gh-28600
1 parent b9f8562 commit 74d1be9

File tree

3 files changed

+22
-84
lines changed

3 files changed

+22
-84
lines changed

spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public void scan(String... basePackages) {
195195
* and if class loading fails (i.e. a {@code ClassNotFoundException} is raised),
196196
* assume the value is a package and attempt to scan it for component classes.
197197
* <p>Enables the default set of annotation configuration post processors, such that
198-
* {@code @Autowired}, {@code @Required}, and associated annotations can be used.
198+
* {@code @Autowired} and associated annotations can be used.
199199
* <p>Configuration class bean definitions are registered with generated bean
200200
* definition names unless the {@code value} attribute is provided to the stereotype
201201
* annotation.

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

Lines changed: 21 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,7 +1168,7 @@ load an entire Spring IoC container instance.
11681168
****
11691169
Since you can mix constructor-based and setter-based DI, it is a good rule of thumb to
11701170
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>>
11721172
annotation on a setter method can be used to make the property be a required dependency;
11731173
however, constructor injection with programmatic validation of arguments is preferable.
11741174
@@ -4600,22 +4600,21 @@ source code and that, in terms of tooling, all configuration styles are supporte
46004600
https://spring.io/tools[Spring Tools for Eclipse].
46014601
****
46024602

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.
46054605
Instead of using XML to describe a bean wiring, the developer moves the configuration
46064606
into the component class itself by using annotations on the relevant class, method, or
46074607
field declaration. As mentioned in <<beans-factory-extension-bpp-examples-aabpp>>, using
46084608
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>>.
46194618

46204619
[NOTE]
46214620
====
@@ -4662,64 +4661,6 @@ it only checks for `@Autowired` beans in your controllers, and not your services
46624661

46634662

46644663

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-
47234664
[[beans-autowired-annotation]]
47244665
=== Using `@Autowired`
47254666

@@ -5026,10 +4967,19 @@ non-required (i.e., by setting the `required` attribute in `@Autowired` to `fals
50264967
}
50274968
----
50284969

4970+
[NOTE]
4971+
====
50294972
A non-required method will not be called at all if its dependency (or one of its
50304973
dependencies, in case of multiple arguments) is not available. A non-required field will
50314974
not get populated at all in such cases, leaving its default value in place.
50324975
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+
50334983
[[beans-autowired-annotation-constructor-resolution]]
50344984

50354985
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`,
50575007
primary/default constructor (if present) will be used. If a class only declares a single
50585008
constructor to begin with, it will always be used, even if not annotated. Note that an
50595009
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.
50675010
====
50685011

50695012
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:
75387481
| -
75397482
| no equivalent
75407483

7541-
| @Required
7542-
| -
7543-
| no equivalent
7544-
75457484
| @Lazy
75467485
| -
75477486
| no equivalent

src/docs/asciidoc/testing.adoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1482,7 +1482,6 @@ and can be used anywhere in the Spring Framework.
14821482
* `@Named` (jakarta.inject) if JSR-330 is present
14831483
* `@PersistenceContext` (jakarta.persistence) if JPA is present
14841484
* `@PersistenceUnit` (jakarta.persistence) if JPA is present
1485-
* `@Required`
14861485
* `@Transactional` (org.springframework.transaction.annotation)
14871486
_with <<testcontext-tx-attribute-support, limited attribute support>>_
14881487

0 commit comments

Comments
 (0)