🤍 전 강의 25% 할인 중 🤍

2024년 상반기를 돌아보고 하반기에도 함께 성장해요!
인프런이 준비한 25% 할인 받으러 가기 >>

  • 카테고리

    질문 & 답변
  • 세부 분야

    백엔드

  • 해결 여부

    해결됨

PrototypeProviderTest 에서 궁금한거 있습니다

22.12.10 20:36 작성 조회수 198

0


[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? (예)
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (아니오)
3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예)

[질문 내용]
여기에 질문 내용을 남겨주세요.

PrototypeProviderTest 에서 @Autowired private ApplicationContext ac; 가 어떻게 작동하는 거예요?

static class ClientBean {
    @Autowired
    private ApplicationContext ac;

과거의 답변을 보니

@Autowired는 스프링 빈도 찾아주지만, 이렇게 ApplicationContext 같은 것도 편리하게 찾을 수 있는 부가 기능도 함께 제공합니다.

그래서 다음 코드는 실패하지만,

ApplicationContext bean = ac.getBean(ApplicationContext.class);

다음 코드는 성공합니다.

@Autowired ApplicationContext ac;

.

라고 하셨는데요 무슨 뜻인지 잘 이해가 안가서요. 필드주입이라는 뜻인가요?

ApplicationContext 같은 것 이 스프링 컨테이너를 말하시는 건가요?

근데 ApplicationContext bean = ac.getBean(ApplicationContext.class); 은 왜 실패하는 거죠?

ac.getBean() 도 찾아서 넣어주는 것아닌가요?

답변 1

답변을 작성해보세요.

3

David님의 프로필

David

2022.12.11

안녕하세요. 코찔질이님, 공식 서포터즈 David입니다.

ApplicationContext는 조금 특별해서 그렇습니다.

일반 빈처럼 컨테이너 내에 빈으로 존재하진 않습니다. 대신, @Autowired 대상이 될 때 내부적으로 ApplicationContext를 주입해줍니다.

이와 관련된 내용은 아래 문서의 registerResolvableDependency 항목을 참고해주세요.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/config/ConfigurableListableBeanFactory.html

Register a special dependency type with corresponding autowired value.

This is intended for factory/context references that are supposed to be autowirable but are not defined as beans in the factory: e.g. a dependency of type ApplicationContext resolved to the ApplicationContext instance that the bean is living in.

Note: There are no such default types registered in a plain BeanFactory, not even for the BeanFactory interface itself.

Parameters:dependencyType - the dependency type to register. This will typically be a base interface such as BeanFactory, with extensions of it resolved as well if declared as an autowiring dependency (e.g. ListableBeanFactory), as long as the given value actually implements the extended interface.autowiredValue - the corresponding autowired value. This may also be an implementation of the ObjectFactory interface, which allows for lazy resolution of the actual target value.

 

감사합니다.

g g님의 프로필

g g

2023.01.03

David님 위에 공식문서 읽으시면 이해 되시나요? 구글 번역기로 돌려도 이해가 전혀 안가서 여쭤봅니다.... 영어 공부를 따로 해야 하나 걱정이 큽니다 흑흑 ㅠㅠ

채널톡 아이콘