• 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

ApplicationContext 필드주입 질문입니다

24.02.28 16:01 작성 24.02.28 16:04 수정 조회수 121

0

applicationContext를 필드 주입 받는 부분입니다

  1. ApplicationContext가 컨테이너 라고 알고있는데 맞나요?

  2. 그러면 ApplicationContext 컨테이너에 PrototypeBean이 등록되어야 getBean(PrototypeBean.class) 를 할 수 있는 거 아닌가요? PrototypeBean을 빈으로 등록하지 않았는데 가능한 이유가 궁금합니다. 혹시 아래 부분에서 PrototypeBean이 등록되는 건가요?

     

  3. 동작원리(순서)가 궁금합니다

            AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class ,PrototypeBean.class);
    

답변 1

답변을 작성해보세요.

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2024.03.01

안녕하세요, 인프런 AI 인턴입니다.

  1. 예, ApplicationContext는 스프링의 특정한 기능을 제공하며, 빈들을 제어하고 관리하는 컨테이너로 알려져 있습니다.

  2. 네, ApplicationContext 컨테이너에 PrototypeBean이 등록되어 있어야 getBean(PrototypeBean.class)를 통해 해당 빈을 조회할 수 있습니다. 제공되신 첫 번째 코드 이미지에 대한 질문 내용으로 추정해 볼 때, AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(ClientBean.class, PrototypeBean.class); 코드라인에서 PrototypeBean 클래스를 파라미터로 전달하여 PrototypeBean을 빈으로 등록하고 있습니다.

  3. 동작 원리는 다음과 같습니다:

    • AnnotationConfigApplicationContext 객체를 생성할 때, ClientBeanPrototypeBean 클래스를 생성자 인자로 전달합니다.
    • 이렇게 함으로써 스프링 컨테이너는 해당 클래스들을 빈으로 등록하고 관리합니다.
    • ClientBean 또는 PrototypeBean의 인스턴스가 필요할 때, ApplicationContext를 통해 getBean() 메소드를 호출함으로써 해당 빈의 인스턴스를 얻을 수 있습니다.
    • PrototypeBean가 프로토타입 스코프로 정의되어 있다면, getBean()을 호출할 때마다 새로운 인스턴스가 생성됩니다. 반면 ClientBean은 싱글턴 스코프로 정의되어 있으므로 같은 인스턴스가 유지됩니다.

더 궁금하신 내용이나 추가 질문이 있으시면 알려주세요.