<빈 출력 방법>
* "AnnotationConfigApplicationContext".getBeanDefinitionNames() : 스프링에 등록된 모든 빈 네임을 조회
* "AnnotationConfigApplicationContext".getBean(빈 이름 , [타입]) : 빈 이름으로 객체 조회
# 스프링 빈 조회 시 같은 타입이 둘 이상이면 에러가 발생한다. -> 네임으로 조회하기!
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
// static으로 import 해준다
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SameBeanConfig.class);
// static으로 생성한 SameBeanConfig(현 코드에는 생략)에서 설정을 읽고 Bean 등록
MemberRepository memberRepository = ac.getBean("memberRepository1", MemberRepository.class);
// getBean(name, class) 형태로 스프링 빈을 찾아온다
assertThat(memberRepository).isInstanceOf(MemberRepository.class);
// 찾아온 스프링 빈이 MemberRepository 클래스인지 확인
assertThrows(NoUniqueBeanDefinitionException.class, ()-> ac.getBean(MemberRepository.class));
// Exception 확인하는 assertThrows (실패 테스트)
Map<String, MemberRepository> beansOfType = ac.getBeansOfType(MemberRepository.class);
// MemberRepository 타입의 Bean을 담아온다
assertThat(beansOfType.size()).isEqualTo(2);
// MemberRepository 타입의 Bean이 2개인지 테스트
<스프링 빈의 상속관계>
- BeanFactory : 스프링 컨테이너의 최상위 인터페이스. 빈을 관리하고 조회한다 ( by getBean() )
- ApplicationContext : BeanFactory의 기능을 상속받아서 제공한다. 뿐만 아니라 MessageSource, EnvironmentCapable, ApplicationEventPublisher, ResourceLoader와 같은 부가기능도 상속받는다)👇
- MessageSource : 국가에 맞는 언어로 출력 (국제화 기능)
- EnvironmentCapable : 개발 환경에 맞게 구분
- ApplicationEventPublisher : 이벤트 발행 & 구독
- ResourceLoader: 외부에서 리소스를 로드해서 편리하게 조회
<스프링 컨테이너 생성 방법>
- Annotation 기반 자바 코드 설정 (현재 사용하는 방법)
- XML 기반 설정 사용 : 컴파일 없이 빈 설정 정보 사용 가능
<스프링 빈 설정 메타 정보>
-> BeanDefinition : 추상화되어있는 상태이며, AppConfig, XML을 이용해 BeanDefintion을 생성한다.
* BeanClassName : 빈을 직접 등록
* FactoryBeanName : 팩토리 역할의 빈을 사용할 경우 ex) appConfig
'Spring' 카테고리의 다른 글
스프링 핵심 원리 - 기본편(빈 스코프) (0) | 2022.04.28 |
---|---|
스프링 핵심 원리 - 기본편(빈 생명주기 콜백) (0) | 2022.04.27 |
스프링 핵심 원리 - 기본편(의존관계 자동 주입) (0) | 2022.04.27 |
스프링 핵심 원리 - 기본편(컴포넌트 스캔과 의존관계 자동 주입) (0) | 2022.04.25 |
스프링 핵심 원리(기본편) - 객체 지향 원리 적용 (0) | 2022.04.17 |