인프런 커뮤니티 질문&답변
applicationContext 주입 관련 질문입니다.(싱글톤 빈 내부 프로토 타입 빈 관련 )
해결된 질문
작성
·
467
2
=========================================
[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? (예)
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예)
3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예)
[질문 내용]
안녕하세요 싱글톤 빈 내부에서 프로토타입 빈을 주입받는 경우에 관하여 질문이 있습니다.
@Scope("singleton")
static class ClientBean {
@Autowired
ApplicationContext applicationContext;
//private final PrototypeBean prototypeBean;
/* @Autowired
public ClientBean(PrototypeBean prototypeBean) {
this.prototypeBean = prototypeBean;
}*/
public int logic() {
PrototypeBean prototypeBean = applicationContext.getBean(PrototypeBean.class);
prototypeBean.addCount();
int count = prototypeBean.getCount();
return count;
/* prototypeBean.addCount();
int count = prototypeBean.getCount();
return count;*/
}
}
강의에서, applicaionContext를 직접 주입받아서, 새로운 prototypeBean을 생성하는 방식을 위와 같이 알려주셨습니다.
(질문)
@Autowired
ApplicationContext applicationContext => 기존의 스프링 컨테이너를 주입받는다고 말씀해주셨습니다.
직관적으로 이해는 가나... 제가 알고 있는 @Autowired 정의와 다소 충돌이 일어나서 아래와 같은 의문이 생깁니다.
저는 @Autowired를 "스프링 컨테이너 내부에 있는 빈 중 type이 일치하는 것을 주입" 이라고 알고 있습니다.
이러한 관점에서 볼 때, applicationContext는 엄연히 빈이 아님에도 어떻게 주입이 가능한지, 문의드립니다.
답변 1
1
안녕하세요. mapth2357님, 공식 서포터즈 OMG입니다.
저도 으레 '스프링 컨테이너에 빈으로 등록' 되어 있겠구나 라고 생각하고 깊게 파보지는 않았는데요,
답변 드리기 위해 찾아보았습니다.
아래 정리글을 참고해주세요.
https://camel-it.tistory.com/8
본문 내용대로 실행 시 아래와 같이 실행되네요.
감사합니다.





