From fa4ecff9e045144a0ab1bb2d2f0d66015e534b5e Mon Sep 17 00:00:00 2001 From: Kazuki Shimizu Date: Fri, 24 Feb 2017 12:04:29 +0900 Subject: [PATCH] Update doc about constructor injection Closes gh-8392 --- .../src/main/asciidoc/using-spring-boot.adoc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc b/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc index abb0f3edf4..2b9016eca8 100644 --- a/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc +++ b/spring-boot-docs/src/main/asciidoc/using-spring-boot.adoc @@ -590,6 +590,24 @@ required `RiskAssessor` bean. } ---- +And if a bean has one constructor, you can omit the `@Autowired`. + +[source,java,indent=0] +---- + @Service + public class DatabaseAccountService implements AccountService { + + private final RiskAssessor riskAssessor; + + public DatabaseAccountService(RiskAssessor riskAssessor) { + this.riskAssessor = riskAssessor; + } + + // ... + + } +---- + TIP: Notice how using constructor injection allows the `riskAssessor` field to be marked as `final`, indicating that it cannot be subsequently changed.