프로젝트에 Main 클래스를 추가하고 스프링이 CommentRepository 빈을 주입하는 방법을 테스트한다. Main 클래스는 다음 예제에서 보여 준다.
예제 5-6 Main 클래스에서 프로토타입 빈을 주입하는 스프링 동작 테스트하기
public class Main {
public static void main(String[] args) {
var c = new AnnotationConfigApplicationContext(ProjectConfig.class);
var s1 = c.getBean(CommentService.class); ← 서비스 빈에 대한 컨텍스트에서 참조를 얻는다.
var s2 = c.getBean(UserService.class); ← 서비스 빈에 대한 컨텍스트에서 참조를 얻는다.
boolean b = ← 주입된 CommentRepository 인스턴스에 대한 참조 값을 비교한다. CommentRepository는 프로토타입 빈이므로 비교 결과는 항상 ‘false’다.
s1.getCommentRepository() == s2.getCommentRepository();
System.out.println(b);
}
}