ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
ApplicationContext(인터페이스)를 스프링 컨테이너라고 한다.
AppConfig.class 정보(애플리케이션 구성정보)를 ApplicationContext에 넘겨준다.
AppConfig 정보를 기반으로 빈 저장소에 AppConfig에 있는 @Bean을 하나씩 저장한다.
형태는 {빈 이름(메서드 이름):빈 객체(메서드 구현체)} 형태로 저장된다. (이름은 모두 다른 이름으로 지정해야한다)
스프링 컨테이너는 설정 정보를 참고해서 의존 관계를 주입(DI) 한다.
스프링 컨테이너에 등록된 Bean 조회
getBean() 메서드 활용
Bean = BeanBeanDefiniton
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
@Test
@DisplayName("모든 빈 출력하기")
void findAllBean() {
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
Object bean = ac.getBean(beanDefinitionName);
System.out.println("name = " + beanDefinitionName + " object = " + bean);
// name = org.springframework.context.annotation.internalConfigurationAnnotationProcessor object = org.springframework.context.annotation.ConfigurationClassPostProcessor@1a9c38eb
// name = org.springframework.context.annotation.internalAutowiredAnnotationProcessor object = org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@319bc845
// name = org.springframework.context.annotation.internalCommonAnnotationProcessor object = org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@4c5474f5
// name = org.springframework.context.event.internalEventListenerProcessor object = org.springframework.context.event.EventListenerMethodProcessor@2f4205be
// name = org.springframework.context.event.internalEventListenerFactory object = org.springframework.context.event.DefaultEventListenerFactory@54e22bdd
// name = appConfig object = hello.core.AppConfig$$EnhancerBySpringCGLIB$$2cd7a939@3bd418e4
// name = memberRepository object = hello.core.member.MemoryMemberRepository@544820b7
// name = discountPolicy object = hello.core.dicount.FixDiscountPolicy@6b98a075
// name = memberService object = hello.core.member.MemberServiceImpl@2e61d218
// name = orderService object = hello.core.order.OrderServiceImpl@3569fc08
}
}
@Test
@DisplayName("모든 빈 출력하기")
void findApplicationBean() {
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);// Bean에 대한 메타데이터
// 스프링 내부에서 등록한 Bean이 아닌 애플리케이션 개발을 위해 추가로 등록한 Bean
if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
Object bean = ac.getBean(beanDefinitionName);
System.out.println("name = " + beanDefinitionName + " object = " + bean);
// name = appConfig object = hello.core.AppConfig$$EnhancerBySpringCGLIB$$2cd7a939@1a9c38eb
// name = memberRepository object = hello.core.member.MemoryMemberRepository@319bc845
// name = discountPolicy object = hello.core.dicount.FixDiscountPolicy@4c5474f5
// name = memberService object = hello.core.member.MemberServiceImpl@2f4205be
// name = orderService object = hello.core.order.OrderServiceImpl@54e22bdd
}
}
}
스프링컨테이너의 빈을 조회하는 방법
- getBean("name", interface class) -> 빈 이름으로 조회
- getBean(interface class) -> 타입으로 조회
- 같은 타입이 2개가 있을 경우 Exception 발생 -> getBeansOfType 사용
Map 형태로 리턴하여 타입에 해당하는 빈들을 return 한다 - getBean("name", implement class) -> 구현체 타입으로 조회
BeanFactory와 ApplicationContext
<<interface>>BeanFactory <-- <<interface>>ApplicationContext <-- AnnotationConfigApplicationContext
BeanFactory?
스프링 컨테이너의 최상위 인터페이스
스프링 빈을 관리하고 조회하는 역할
ApplicationContext?
- BeanFactory의 기능을 모두 상속받아서 제공 +
- 메시지 소스를 활용한 국제화 기능
- 환경변수
- 애플리케이션 이벤트
- 편리한 리소스 조회 등...
ApplicationContext는 BeanFactory의 기능을 상속 받음
ApplicationContext는 빈 관리기능 + 편리한 부가기능을 제공
BeanFactory를 직접 사용할 일은 거의 없고 당연히 부가기능이 포함된 ApplicationContext를 주로 사용할 것
BeanFactory나 ApplicationContext를 스프링 컨테이너라고 한다.
'SpringFramework > Spring 중요 개념' 카테고리의 다른 글
[Spring] @Configuration과 싱글톤 (0) | 2021.12.17 |
---|---|
[Spring] 싱글톤 컨테이너 (0) | 2021.12.17 |
[Spring] 스프링으로 변환하기 (0) | 2021.12.15 |
[Spring] IOC, DI, 그리고 컨테이너 (0) | 2021.12.15 |
[Spring] 객체지향원리 개념 (DI) (0) | 2021.12.15 |
댓글