더북(TheBook)

우리가 작업하는 모든 단계를 유지하기 위해 첫 번째 시연에서 sq-ch4-ex3이라는 새 프로젝트를 만든다. 다행히도 변경해야 하는 것은 CommentService 클래스뿐이다. 생성자를 제거하고 다음 코드에서 표시된 대로 클래스의 필드를 @Autowired 애너테이션으로 표시한다.

@Component
public class CommentService {

    @Autowired ← 필드는 이제 final이 아니며, @Autowired로 표시된다. 스프링은 기본 생성자로 클래스의 인스턴스를 생성한 후 스프링의 컨텍스트에서 두 의존성을 가져와 주입한다.
    private CommentRepository commentRepository;

    @Autowired 
    private CommentNotificationProxy commentNotificationProxy; ← 필드는 이제 final이 아니며, @Autowired로 표시된다. 스프링은 기본 생성자로 클래스의 인스턴스를 생성한 후 스프링의 컨텍스트에서 두 의존성을 가져와 주입한다.
    public void publishComment(Comment comment) {
        commentRepository.storeComment(comment);
        commentNotificationProxy.sendComment(comment);
    }
}
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.