Skip to content

Commit 20bb9b6

Browse files
kwoykepivovarit
authored andcommitted
BAEL-3688: Add examples of a constructor and a setter injection types (eugenp#8452)
1 parent 3140084 commit 20bb9b6

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

spring-core-2/src/main/java/com/baeldung/autowire/sample/FooService.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,27 @@
33
import org.springframework.beans.factory.annotation.Autowired;
44
import org.springframework.stereotype.Component;
55

6+
/**
7+
* This class presents a field, a constructor, and a setter injection type.
8+
* Usually, we'd stick with a single approach for a given property. This is just an educational code.
9+
*/
610
@Component
711
public class FooService {
812

913
@Autowired
1014
@FormatterType("Foo")
1115
private Formatter formatter;
1216

17+
@Autowired
18+
public FooService(@FormatterType("Foo") Formatter formatter) {
19+
this.formatter = formatter;
20+
}
21+
22+
@Autowired
23+
public void setFormatter(@FormatterType("Foo") Formatter formatter) {
24+
this.formatter = formatter;
25+
}
26+
1327
public String doStuff() {
1428
return formatter.format();
1529
}

0 commit comments

Comments
 (0)