-
6월 7일 수요일 TIL 회고록카테고리 없음 2023. 6. 7. 21:18
스프링 컨테이너 생성
스프링 컨테이너가 생성되는 과정
// 스프링 컨테이너 생성 ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
- 'ApplicationContext'를 스프링 컨테이너라 한다.
- 'ApplicationContext'는 인터페이스이다.
- 스프링 컨테이너는 XML을 기반으로 만들 수 있고, 애노테이션 기반의 자바 설정 클래스로 만들 수 있다. (요즘에는 XML을 기반으로 만들지는 않는다. 어노테이션 기반으로 만드는게 더욱 편리하기 때문)
- 자바 설정 클래스를 기반으로 스프링 컨테이너("ApplicationContext')를 만들어보자.
- 'new AnnotationConfigApplicationContext(AppConfig.class);'
- 이 클래스는 'ApplicationContext' 인터페이스의 구현체이다.
참고: 더 정확히는 스프링 컨테이너를 부를 때 'BeanFactory', 'ApplicationContext'로 구분해서 이야기한다. 'BeanFactory'를 직접 사용하는 경우는 거의 없으므로 일반적으로 'ApplicationContext'를 스프링 컨테이너라 한다.
스프링 컨테이너의 생성 과정
1. 스프링 컨테이너 생성
- 'new AnnotationConfigApplicationContext(AppConfig.class)'
- new AnnotationConfigApplicationContext 라고 하면서 AppConfig.class의 정보를 준다. (1번)
- 그러면 스프링 컨테이너가 만들어진다. 그런데 스프링 컨테이너 안에는 스프링 빈 저장소라는게 있다.
- Key는 빈의 이름이 되고, Value는 빈의 객체가 된다.
- 스프링 컨테이너를 생성할 때는 구성 정보를 지정해주어야 한다.
- 여기서는 'AppConfig.class'를 구성 정보로 지정했다.
2. 스프링 빈 등록
- 스프링 컨테이너는 파라미터로 넘어온 설정 클래스 정보를 사용해서 스프링 빈을 등록한다.
- @Bean이 붙은 메서드들을 본 후 전부 호출을 한다.
- 그 후 메서드 이름을 Key (빈 이름)으로 가지고 이거에 대한 값은 return new **** < 이 객체를 빈 객체로 등록을 해 준다.
- 이것을 스프링 빈이라고 한다.
빈 이름
- 빈 이름은 메서드 이름을 사용한다.
- 빈 이름을 직접 부여할 수 도 있다.
- @Bean(name="memberService2")
주의: 빈 이름은 항상 다른 이름을 부여해야 한다. 같은 이름을 부여하면, 다른 빈이 무시되거나, 기존 빈을 덮어버리거나 설정에 따라 오류가 발생한다.
3. 스프링 빈 의존관계 설정 - 준비
- 스프링 빈들을 그림으로 표현하였다.
4. 스프링 빈 의존관계 설정 - 완료
- 스프링 컨테이너는 설정 정보를 참고해서 의존관계를 주입(DI) 한다.
- MemberSerivce의 의존관계를 memberRepository에 넣어주었다. 넣어줌으로써 MemoryMemberRepository가 세팅이 된다.
- orderService는 memberRepository도 의존하고 discountPolicy도 의존한다.
- discountPolicy는 실제로는 RateDiscountPolicy가 호출된다.
- 동적인 객체 인스턴스 의존관계를 실제로 스프링이 전부 연결 해 준다. 그래서 실제 스프링이 연결이 된다. (객체의 참조값들이 연결이 된다.)
스프링은 빈을 생성하고, 의존관계를 주입하는 단계가 나누어져 있다. 그런데 이렇게 자바 코드로 스프링 빈을 등록하면 생성자를 호출하면서 의존관계 주입도 한번에 처리된다.
컨테이너의 등록된 모든 빈 조회
스프링 컨테이너에 실제 스프링 빈들이 잘 등록 되었는지 테스트 코드를 작성하여 확인해보자.
먼저 스프링 컨테이너를 생성한다.
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
그 다음 findAllBean 메서드를 만들어준다.
테스트 코드라서 Test 어노테이션과 DisplayName 어노테이션으로 테스트코드를 보기 편하게 만들어준다.
@Test @DisplayName("모든 빈 출력하기") void findAllBean() { }
ac.getBeanDefinitionNames()으로 스프링에 등록된 모든 빈 이름을 조회할 수 있다.
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
그 다음 beanDefinitionNames를 반복문을 사용해 값을 꺼내오고 Object bean = ac.getBean(beanDefinitionName)을 사용해 빈 이름으로 빈 객체(인스턴스)를 조회한다. 타입이 Object인 이유는 타입을 지정 하지 않았기 때문이다.
그 다음 System.out.println으로 모든 빈을 출력한다.
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@5a9f4771 name = org.springframework.context.annotation.internalAutowiredAnnotationProcessor object = org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@282cb7c7 name = org.springframework.context.annotation.internalCommonAnnotationProcessor object = org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@7d898981 name = org.springframework.context.event.internalEventListenerProcessor object = org.springframework.context.event.EventListenerMethodProcessor@48d61b48 name = org.springframework.context.event.internalEventListenerFactory object = org.springframework.context.event.DefaultEventListenerFactory@68d279ec name = appConfig object = hello.core.AppConfig$$SpringCGLIB$$0@258d79be name = memberService object = hello.core.member.MemberServiceImpl@14f9390f name = memberRepository object = hello.core.member.MemoryMemberRepository@6c0d7c83 name = orderService object = hello.core.order.OrderServiceImpl@176b75f7 name = discountPolicy object = hello.core.discount.RateDiscountPolicy@5965be2d
위에 실행 결과에서 아래 5개는 스프링 내부적으로 스프링 자체를 확장하기 위해서 쓰는 빈들이다.
name = org.springframework.context.annotation.internalConfigurationAnnotationProcessor object = org.springframework.context.annotation.ConfigurationClassPostProcessor@5a9f4771 name = org.springframework.context.annotation.internalAutowiredAnnotationProcessor object = org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@282cb7c7 name = org.springframework.context.annotation.internalCommonAnnotationProcessor object = org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@7d898981 name = org.springframework.context.event.internalEventListenerProcessor object = org.springframework.context.event.EventListenerMethodProcessor@48d61b48 name = org.springframework.context.event.internalEventListenerFactory object = org.springframework.context.event.DefaultEventListenerFactory@68d279ec
appConfig도 스프링 빈으로 등록이 된다.
appConfig를 제외한 나머지 4개의 빈이 진짜 등록한 빈들이다.
name = appConfig object = hello.core.AppConfig$$SpringCGLIB$$0@258d79be name = memberService object = hello.core.member.MemberServiceImpl@14f9390f name = memberRepository object = hello.core.member.MemoryMemberRepository@6c0d7c83 name = orderService object = hello.core.order.OrderServiceImpl@176b75f7 name = discountPolicy object = hello.core.discount.RateDiscountPolicy@5965be2d
위에 테스트 코드는 내가 등록한 빈들만 나오는게 아닌 스프링 내부적으로 등록한 빈들도 같이 나온다.
이번에는 내가 등록한 빈들만 나오게 테스트 코드를 만들어보자.
이번에는 findApplicationBean 메서드를 만들었다.
@Test @DisplayName("애플리케이션 빈 출력하기") void findApplicationBean() { }
전체 빈을 출력하는 테스트 코드와 같이 ac.getBeanDefinitionNames를 사용해 스프링에 모든 빈 이름을 조회한다.
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
그 다음 반복문을 사용해 값을 꺼내오고
for (String beanDefinitionName : beanDefinitionNames) { }
getBeanDefinition(beanDefinitionName을 사용한다.
getBeanDefinition : 빈 하나하나에 대한 정보들, 메타 데이터들을 꺼낼 수 있다.
BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);
그 다음 if문을 사용해 beanDefinition.getRole()이 BeanDefinition.ROLE_APPLICATION 이면
Object bean = ac.getBean(beanDefinitionName)을 사용해 빈 이름으로 빈 객체(인스턴스)를 조회한다.
그 다음 System.out.println으로 빈을 출력한다.
// Role ROLE_APPLICATION : 직접 등록한 애플리케이션 빈 // Role ROLE_INFRASTRUCTURE : 스프링이 내부에서 사용하는 빈 if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) { Object bean = ac.getBean(beanDefinitionName); System.out.println("name = " + beanDefinitionName + " object = " + bean); } }
실행 결과
name = appConfig object = hello.core.AppConfig$$SpringCGLIB$$0@5a9f4771 name = memberService object = hello.core.member.MemberServiceImpl@282cb7c7 name = memberRepository object = hello.core.member.MemoryMemberRepository@7d898981 name = orderService object = hello.core.order.OrderServiceImpl@48d61b48 name = discountPolicy object = hello.core.discount.RateDiscountPolicy@68d279ec
내가 등록한 빈들만 출력이 되었다.
정리
- 모든 빈 출력하기
- 실행하면 스프링에 등록된 모든 빈 정보를 출력할 수 있다.
- ac.getBeanDefinitionNames() : 스프링에 등록된 모든 빈 이름을 조회한다.
- ac.getBean() : 빈 이름으로 빈 객체(인스턴스)를 조회한다.
- 애플리케이션 빈 출력하기
- 스프링이 내부에서 사용하는 빈은 제외하고, 내가 등록한 빈만 출력해보기
- 스프링이 내부에서 사용하는 빈은 getRole()로 구분할 수 있다.
- ROLE_APPLICATION : 일반적으로 사용자가 정의한 빈
- ROLE_INFRASTRUCTURE : 스프링이 내부에서 사용하는 빈
출처 - 인프런 김영한 강사님의 스프링 핵심 원리 - 기본편