BAEL-3688: Add examples of a constructor and a setter injection types (#8452)

This commit is contained in:
kwoyke 2020-01-03 07:49:52 +01:00 committed by Grzegorz Piwowarek
parent 63397e4daa
commit 130ba52379
1 changed files with 14 additions and 0 deletions

View File

@ -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();
}