더북(TheBook)

스프링이 이 중 하나를 주입하도록 하려면 @Qualifier 애너테이션으로 구현 이름을 다시 지정하기만 하면 된다. 다음 예제에서 특정 구현을 CommentService 객체의 의존성으로 주입하는 방법을 알아보자.

예제 4-15 @Qualifier로 스프링이 주입해야 하는 구현 지정하기

@Component
public class CommentService {

    private final CommentRepository commentRepository;
    private final CommentNotificationProxy commentNotificationProxy;

    public CommentService( ← 특정 구현을 사용하려는 매개변수에서는 @Qualifier로 애너테이션을 추가한다.
           CommentRepository commentRepository,
           @Qualifier("PUSH") CommentNotificationProxy commentNotificationProxy) {

        this.commentRepository = commentRepository;
        this.commentNotificationProxy = commentNotificationProxy;
    }

    // 코드 생략
}
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.