@ -4168,18 +4168,24 @@ references and values even when you use the class outside of a container.
@@ -4168,18 +4168,24 @@ references and values even when you use the class outside of a container.
[[beans-autowired-annotation]]
=== @Autowired
As expected, you can apply the `@Autowired` annotation to "traditional" setter methods:
[NOTE]
====
JSR 330's `@Inject` annotation can be used in place of Spring's `@Autowired` annotation
in the examples below. See <<beans-standard-annotations,here>> for more details.
====
You can apply the `@Autowired` annotation to constructors:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
public class SimpleMovieLister {
public class MovieRecommender {
private MovieFinder movieFinder;
private final CustomerPreferenceDao customerPreferenceDao;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
@ -4189,10 +4195,31 @@ As expected, you can apply the `@Autowired` annotation to "traditional" setter m
@@ -4189,10 +4195,31 @@ As expected, you can apply the `@Autowired` annotation to "traditional" setter m
[NOTE]
====
JSR 330's `@Inject` annotation can be used in place of Spring's `@Autowired` annotation
in the examples below. See <<beans-standard-annotations,here>> for more details.
As of Spring Framework 4.3, the `@Autowired` constructor is no longer necessary if the
target bean only defines one constructor. If several constructors are available, at
least one must be annotated to teach the container which one it has to use.
====
As expected, you can also apply the `@Autowired` annotation to "traditional" setter
methods:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
public class SimpleMovieLister {
private MovieFinder movieFinder;
@Autowired
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
// ...
}
----
You can also apply the annotation to methods with arbitrary names and/or multiple