setter 주입을 사용한 예제는 sq-ch3-ex6 프로젝트에서 확인할 수 있으며, Person 클래스만 변경하면 이를 구현할 수 있다는 것을 알 수 있다. 다음 코드에서는 setter에 @Autowired 애너테이션을 사용했다.
@Component
public class Person {
private String name = "Ella";
private Parrot parrot;
// getters와 setters 생략
@Autowired
public void setParrot(Parrot parrot) {
this.parrot = parrot;
}
}
앱을 실행하면 다음과 같이 이전에 설명한 예제와 출력이 동일하다.
Person's name: Ella Person's parrot: Parrot : Koko