@@ -4168,18 +4168,24 @@ references and values even when you use the class outside of a container.
4168
4168
[[beans-autowired-annotation]]
4169
4169
=== @Autowired
4170
4170
4171
- As expected, you can apply the `@Autowired` annotation to "traditional" setter methods:
4171
+ [NOTE]
4172
+ ====
4173
+ JSR 330's `@Inject` annotation can be used in place of Spring's `@Autowired` annotation
4174
+ in the examples below. See <<beans-standard-annotations,here>> for more details.
4175
+ ====
4176
+
4177
+ You can apply the `@Autowired` annotation to constructors:
4172
4178
4173
4179
[source,java,indent=0]
4174
4180
[subs="verbatim,quotes"]
4175
4181
----
4176
- public class SimpleMovieLister {
4182
+ public class MovieRecommender {
4177
4183
4178
- private MovieFinder movieFinder ;
4184
+ private final CustomerPreferenceDao customerPreferenceDao ;
4179
4185
4180
4186
@Autowired
4181
- public void setMovieFinder(MovieFinder movieFinder ) {
4182
- this.movieFinder = movieFinder ;
4187
+ public MovieRecommender(CustomerPreferenceDao customerPreferenceDao ) {
4188
+ this.customerPreferenceDao = customerPreferenceDao ;
4183
4189
}
4184
4190
4185
4191
// ...
@@ -4189,10 +4195,31 @@ As expected, you can apply the `@Autowired` annotation to "traditional" setter m
4189
4195
4190
4196
[NOTE]
4191
4197
====
4192
- JSR 330's `@Inject` annotation can be used in place of Spring's `@Autowired` annotation
4193
- in the examples below. See <<beans-standard-annotations,here>> for more details.
4198
+ As of Spring Framework 4.3, the `@Autowired` constructor is no longer necessary if the
4199
+ target bean only defines one constructor. If several constructors are available, at
4200
+ least one must be annotated to teach the container which one it has to use.
4194
4201
====
4195
4202
4203
+ As expected, you can also apply the `@Autowired` annotation to "traditional" setter
4204
+ methods:
4205
+
4206
+ [source,java,indent=0]
4207
+ [subs="verbatim,quotes"]
4208
+ ----
4209
+ public class SimpleMovieLister {
4210
+
4211
+ private MovieFinder movieFinder;
4212
+
4213
+ @Autowired
4214
+ public void setMovieFinder(MovieFinder movieFinder) {
4215
+ this.movieFinder = movieFinder;
4216
+ }
4217
+
4218
+ // ...
4219
+
4220
+ }
4221
+ ----
4222
+
4196
4223
You can also apply the annotation to methods with arbitrary names and/or multiple
4197
4224
arguments:
4198
4225
@@ -4217,18 +4244,18 @@ arguments:
4217
4244
}
4218
4245
----
4219
4246
4220
- You can apply `@Autowired` to constructors and fields :
4247
+ You can apply `@Autowired` to fields as well and even mix it with constructors :
4221
4248
4222
4249
[source,java,indent=0]
4223
4250
[subs="verbatim,quotes"]
4224
4251
----
4225
4252
public class MovieRecommender {
4226
4253
4254
+ private final CustomerPreferenceDao customerPreferenceDao;
4255
+
4227
4256
@Autowired
4228
4257
private MovieCatalog movieCatalog;
4229
4258
4230
- private CustomerPreferenceDao customerPreferenceDao;
4231
-
4232
4259
@Autowired
4233
4260
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
4234
4261
this.customerPreferenceDao = customerPreferenceDao;
0 commit comments