더북(TheBook)

다음 코드처럼 Main 클래스를 변경하고 CommentService 빈에 대한 참조를 추가한다.

public class Main {

    public static void main(String[] args) {
        var c = new AnnotationConfigApplicationContext(ProjectConfig.class);

        System.out.println("Before retrieving the CommentService");
        var service = c.getBean(CommentService.class); ← 이 코드 줄에서 스프링은 CommentService 빈의 참조 값을 제공해야 한다. 이때 빈 인스턴스도 생성한다.
        System.out.println("After retrieving the CommentService");
    }
}

앱을 다시 실행하면 콘솔에서 출력을 확인할 수 있다. 프레임워크는 빈이 사용될 때만 빈을 생성한다.

Before retrieving the CommentService
CommentService instance created!
After retrieving the CommentService
신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.