스프링 컨테이너 생성


- 빈의 이름은 항상 다른 이름을 부여해야한다.
컨테이너에 등록된 모든 빈 조회
public class ApplicationContextInfoTest {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
@Test
@DisplayName("모든 빈 출력하기")
void findAllBean() {
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
for (String beanDefinationName : beanDefinitionNames) {
Object bean = ac.getBean(beanDefinationName);
System.out.println("name = " + beanDefinationName + " object = " + bean);
}
}
}
name = org.springframework.context.annotation.internalConfigurationAnnotationProcessor object = org.springframework.context.annotation.ConfigurationClassPostProcessor@42d236fb
name = org.springframework.context.annotation.internalAutowiredAnnotationProcessor object = org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@60e9df3c
name = org.springframework.context.annotation.internalCommonAnnotationProcessor object = org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@907f2b7
name = org.springframework.context.event.internalEventListenerProcessor object = org.springframework.context.event.EventListenerMethodProcessor@435ce306
name = org.springframework.context.event.internalEventListenerFactory object = org.springframework.context.event.DefaultEventListenerFactory@537b32ef
name = appConfig object = hello.hellospring.config.AppConfig$$SpringCGLIB$$0@7dc51783
name = memberService object = hello.hellospring.service.MemberServiceImpl@4b61d0c6
name = memberRepository object = hello.hellospring.repository.MemoryMemberRepository@6f815e7f
name = orderService object = hello.hellospring.order.OrderServiceImpl@65e7f52a
name = discountPolicy object = hello.hellospring.discount.RateDiscountPolicy@304b9f1a
애플리케이션 빈 출력하기
@Test
@DisplayName("애플리케이션 빈 출력하기")
void findApplicationBean() {
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
for (String beanDefinationName : beanDefinitionNames) {
BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinationName);
if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
Object bean = ac.getBean(beanDefinationName);
System.out.println("name = " + beanDefinationName + " object = " + bean);
}
name = appConfig object = hello.hellospring.config.AppConfig$$SpringCGLIB$$0@537b32ef
name = memberService object = hello.hellospring.service.MemberServiceImpl@78f9ed3e
name = memberRepository object = hello.hellospring.repository.MemoryMemberRepository@1059754c
name = orderService object = hello.hellospring.order.OrderServiceImpl@b0964b2
name = discountPolicy object = hello.hellospring.discount.RateDiscountPolicy@48e7b3d2
스프링 빈 조회 - 기본
- ac.getBean(빈이름, 타입)
- ac.getBean(타입)
- 조회 대상 스프링 빈이 없으면 예외 발생 : NoSuchBeanDefinitionException
@Test
@DisplayName("빈 이름으로 조회")
void findBeanByName() {
MemberService memberService = ac.getBean("memberService", MemberService.class);
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
System.out.println("memberService = " + memberService);
System.out.println("memberService.getClass() = " + memberService.getClass());
Assertions.assertThat(memberService).isInstanceOf(MemberService.class)
}
memberService = hello.hellospring.service.MemberServiceImpl@42d236fb
memberService.getClass() = class hello.hellospring.service.MemberServiceImpl
스프링 빈 조회 - 동일한 타입이 둘 이상
BeanFactory와 ApplicationContext
다양한 설정 형식 지원 - 자바 코드, XML
스프링 빈 설정 메타 정보 - BeanDefinition
ctrl + e 이전 파일로 이동