예제 4-10 CommentService를 컴포넌트로 만들기
@Component ← 스프링은 이 클래스를 빈으로 생성하고 컨텍스트에 추가한다.
public class CommentService {
private final CommentRepository commentRepository;
private final CommentNotificationProxy commentNotificationProxy; ← 이 클래스에 생성자가 두 개 이상 있다면 @Autowired를 사용해야 한다.
public CommentService( ← 스프링은 이 생성자로 빈을 생성하며, 빈 인스턴스를 생성할 때 스프링 컨텍스트에서 매개변수의 레퍼런스(또는 레퍼런스들)를 주입한다.
CommentRepository commentRepository,
CommentNotificationProxy commentNotificationProxy) {
this.commentRepository = commentRepository;
this.commentNotificationProxy = commentNotificationProxy;
}
public void publishComment(Comment comment) {
commentRepository.storeComment(comment);
commentNotificationProxy.sendComment(comment);
}
}