Skip to content

Commit edd8e2d

Browse files
committed
Update documentation
Issue: SPR-12278
1 parent 19d97c4 commit edd8e2d

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

src/asciidoc/core-beans.adoc

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4168,18 +4168,24 @@ references and values even when you use the class outside of a container.
41684168
[[beans-autowired-annotation]]
41694169
=== @Autowired
41704170

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:
41724178

41734179
[source,java,indent=0]
41744180
[subs="verbatim,quotes"]
41754181
----
4176-
public class SimpleMovieLister {
4182+
public class MovieRecommender {
41774183
4178-
private MovieFinder movieFinder;
4184+
private final CustomerPreferenceDao customerPreferenceDao;
41794185
41804186
@Autowired
4181-
public void setMovieFinder(MovieFinder movieFinder) {
4182-
this.movieFinder = movieFinder;
4187+
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
4188+
this.customerPreferenceDao = customerPreferenceDao;
41834189
}
41844190
41854191
// ...
@@ -4189,10 +4195,31 @@ As expected, you can apply the `@Autowired` annotation to "traditional" setter m
41894195

41904196
[NOTE]
41914197
====
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.
41944201
====
41954202

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+
41964223
You can also apply the annotation to methods with arbitrary names and/or multiple
41974224
arguments:
41984225

@@ -4217,18 +4244,18 @@ arguments:
42174244
}
42184245
----
42194246

4220-
You can apply `@Autowired` to constructors and fields:
4247+
You can apply `@Autowired` to fields as well and even mix it with constructors:
42214248

42224249
[source,java,indent=0]
42234250
[subs="verbatim,quotes"]
42244251
----
42254252
public class MovieRecommender {
42264253
4254+
private final CustomerPreferenceDao customerPreferenceDao;
4255+
42274256
@Autowired
42284257
private MovieCatalog movieCatalog;
42294258
4230-
private CustomerPreferenceDao customerPreferenceDao;
4231-
42324259
@Autowired
42334260
public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) {
42344261
this.customerPreferenceDao = customerPreferenceDao;

src/asciidoc/whats-new.adoc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,12 @@ public @interface MyTestConfig {
627627
[[new-in-4.3]]
628628
== New Features and Enhancements in Spring Framework 4.3
629629

630+
=== Core Container Improvements
631+
632+
* It is no longer necessary to specify the `@Autowired` annotation if the target
633+
bean only define one constructor.
634+
635+
630636
=== Testing Improvements
631637

632638
* The JUnit support in the _Spring TestContext Framework_ now requires JUnit 4.12 or higher.

0 commit comments

Comments
 (0)