|
|
@ -4168,18 +4168,24 @@ references and values even when you use the class outside of a container. |
|
|
|
[[beans-autowired-annotation]] |
|
|
|
[[beans-autowired-annotation]] |
|
|
|
=== @Autowired |
|
|
|
=== @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] |
|
|
|
[source,java,indent=0] |
|
|
|
[subs="verbatim,quotes"] |
|
|
|
[subs="verbatim,quotes"] |
|
|
|
---- |
|
|
|
---- |
|
|
|
public class SimpleMovieLister { |
|
|
|
public class MovieRecommender { |
|
|
|
|
|
|
|
|
|
|
|
private MovieFinder movieFinder; |
|
|
|
private final CustomerPreferenceDao customerPreferenceDao; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
public void setMovieFinder(MovieFinder movieFinder) { |
|
|
|
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) { |
|
|
|
this.movieFinder = movieFinder; |
|
|
|
this.customerPreferenceDao = customerPreferenceDao; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ... |
|
|
|
// ... |
|
|
@ -4189,10 +4195,31 @@ As expected, you can apply the `@Autowired` annotation to "traditional" setter m |
|
|
|
|
|
|
|
|
|
|
|
[NOTE] |
|
|
|
[NOTE] |
|
|
|
==== |
|
|
|
==== |
|
|
|
JSR 330's `@Inject` annotation can be used in place of Spring's `@Autowired` annotation |
|
|
|
As of Spring Framework 4.3, the `@Autowired` constructor is no longer necessary if the |
|
|
|
in the examples below. See <<beans-standard-annotations,here>> for more details. |
|
|
|
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 |
|
|
|
You can also apply the annotation to methods with arbitrary names and/or multiple |
|
|
|
arguments: |
|
|
|
arguments: |
|
|
|
|
|
|
|
|
|
|
@ -4217,18 +4244,18 @@ arguments: |
|
|
|
} |
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
|
|
|
|
|
|
|
|
You can apply `@Autowired` to constructors and fields: |
|
|
|
You can apply `@Autowired` to fields as well and even mix it with constructors: |
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0] |
|
|
|
[source,java,indent=0] |
|
|
|
[subs="verbatim,quotes"] |
|
|
|
[subs="verbatim,quotes"] |
|
|
|
---- |
|
|
|
---- |
|
|
|
public class MovieRecommender { |
|
|
|
public class MovieRecommender { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private final CustomerPreferenceDao customerPreferenceDao; |
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
private MovieCatalog movieCatalog; |
|
|
|
private MovieCatalog movieCatalog; |
|
|
|
|
|
|
|
|
|
|
|
private CustomerPreferenceDao customerPreferenceDao; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Autowired |
|
|
|
@Autowired |
|
|
|
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) { |
|
|
|
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) { |
|
|
|
this.customerPreferenceDao = customerPreferenceDao; |
|
|
|
this.customerPreferenceDao = customerPreferenceDao; |
|
|
|