인프런 커뮤니티 질문&답변
ApplicationContext 필드주입 질문입니다
해결된 질문
작성
·
258
·
수정됨
0


applicationContext를 필드 주입 받는 부분입니다
ApplicationContext가 컨테이너 라고 알고있는데 맞나요?
그러면 ApplicationContext 컨테이너에 PrototypeBean이 등록되어야 getBean(PrototypeBean.class) 를 할 수 있는 거 아닌가요? PrototypeBean을 빈으로 등록하지 않았는데 가능한 이유가 궁금합니다. 혹시 아래 부분에서 PrototypeBean이 등록되는 건가요?
동작원리(순서)가 궁금합니다
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class ,PrototypeBean.class);
답변 1
0
안녕하세요, 인프런 AI 인턴입니다.
예,
ApplicationContext는 스프링의 특정한 기능을 제공하며, 빈들을 제어하고 관리하는 컨테이너로 알려져 있습니다.네,
ApplicationContext컨테이너에PrototypeBean이 등록되어 있어야getBean(PrototypeBean.class)를 통해 해당 빈을 조회할 수 있습니다. 제공되신 첫 번째 코드 이미지에 대한 질문 내용으로 추정해 볼 때,AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class, PrototypeBean.class);코드라인에서PrototypeBean클래스를 파라미터로 전달하여PrototypeBean을 빈으로 등록하고 있습니다.동작 원리는 다음과 같습니다:
AnnotationConfigApplicationContext객체를 생성할 때,ClientBean과PrototypeBean클래스를 생성자 인자로 전달합니다.
- 이렇게 함으로써 스프링 컨테이너는 해당 클래스들을 빈으로 등록하고 관리합니다.
ClientBean또는PrototypeBean의 인스턴스가 필요할 때,ApplicationContext를 통해getBean()메소드를 호출함으로써 해당 빈의 인스턴스를 얻을 수 있습니다.
PrototypeBean가 프로토타입 스코프로 정의되어 있다면,getBean()을 호출할 때마다 새로운 인스턴스가 생성됩니다. 반면ClientBean은 싱글턴 스코프로 정의되어 있으므로 같은 인스턴스가 유지됩니다.
더 궁금하신 내용이나 추가 질문이 있으시면 알려주세요.





