From 130ba52379326a06e05e7d3613afb8ba6992e6e8 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Fri, 3 Jan 2020 07:49:52 +0100 Subject: [PATCH] BAEL-3688: Add examples of a constructor and a setter injection types (#8452) --- .../com/baeldung/autowire/sample/FooService.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spring-core-2/src/main/java/com/baeldung/autowire/sample/FooService.java b/spring-core-2/src/main/java/com/baeldung/autowire/sample/FooService.java index c55d93da11..8a073279bc 100644 --- a/spring-core-2/src/main/java/com/baeldung/autowire/sample/FooService.java +++ b/spring-core-2/src/main/java/com/baeldung/autowire/sample/FooService.java @@ -3,6 +3,10 @@ package com.baeldung.autowire.sample; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +/** + * This class presents a field, a constructor, and a setter injection type. + * Usually, we'd stick with a single approach for a given property. This is just an educational code. + */ @Component public class FooService { @@ -10,6 +14,16 @@ public class FooService { @FormatterType("Foo") private Formatter formatter; + @Autowired + public FooService(@FormatterType("Foo") Formatter formatter) { + this.formatter = formatter; + } + + @Autowired + public void setFormatter(@FormatterType("Foo") Formatter formatter) { + this.formatter = formatter; + } + public String doStuff() { return formatter.format(); }