다음 코드는 CommentService 클래스의 정의를 보여 준다. @Autowired를 사용하여 스프링이 클래스에서 선언된 속성에 CommentRepository 타입의 인스턴스를 주입하도록 지시한 것을 볼 수 있다. 또 스프링이 두 서비스 빈에 동일한 객체 참조를 주입한다는 것을 검증하는 데 사용할 getter 메서드도 정의했다.
@Service
public class CommentService {
@Autowired
private CommentRepository commentRepository;
public CommentRepository getCommentRepository() {
return commentRepository;
}
}