diff --git a/src/asciidoc/core-beans.adoc b/src/asciidoc/core-beans.adoc index 98b5c7ee17..8c505c4d38 100644 --- a/src/asciidoc/core-beans.adoc +++ b/src/asciidoc/core-beans.adoc @@ -4168,7 +4168,40 @@ 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 <> for more details. +==== + +You can apply the `@Autowired` annotation to constructors: + +[source,java,indent=0] +[subs="verbatim,quotes"] +---- + public class MovieRecommender { + + private final CustomerPreferenceDao customerPreferenceDao; + + @Autowired + public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) { + this.customerPreferenceDao = customerPreferenceDao; + } + + // ... + + } +---- + +[NOTE] +==== +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"] @@ -4187,12 +4220,6 @@ 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 <> for more details. -==== - You can also apply the annotation to methods with arbitrary names and/or multiple 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] [subs="verbatim,quotes"] ---- public class MovieRecommender { + private final CustomerPreferenceDao customerPreferenceDao; + @Autowired private MovieCatalog movieCatalog; - private CustomerPreferenceDao customerPreferenceDao; - @Autowired public MovieRecommender(CustomerPreferenceDao customerPreferenceDao) { this.customerPreferenceDao = customerPreferenceDao; diff --git a/src/asciidoc/whats-new.adoc b/src/asciidoc/whats-new.adoc index 53d7701f47..ff1a2a0362 100644 --- a/src/asciidoc/whats-new.adoc +++ b/src/asciidoc/whats-new.adoc @@ -627,6 +627,12 @@ public @interface MyTestConfig { [[new-in-4.3]] == New Features and Enhancements in Spring Framework 4.3 +=== Core Container Improvements + +* It is no longer necessary to specify the `@Autowired` annotation if the target + bean only define one constructor. + + === Testing Improvements * The JUnit support in the _Spring TestContext Framework_ now requires JUnit 4.12 or higher.