Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

컨테이너에 등록된 빈 조회 본문

인프런/[인프런] Spring 핵심원리 이해

컨테이너에 등록된 빈 조회

Seung__ 2021. 7. 26. 20:03

스프링에 등록된 빈을 확인할 수 있나???

 

1. 스프링에 등록된 빈을 확인해보자.


아래와 같이 테스트 코드를 작성해 주자.

  • AnnotationConfigApplicationContext(AppConfig.class)를 통해 AppConfig에서
  • @Bean이 붙은 메서드의 반환 객체를 bean으로 등록한다.
  • Bean으로 등록될 객체 :
    AppConfig, MemberServiceImpl, OrderServiceImpl, MemoryMemberRepository, RateDiscountPolicy.
public class ApplicationContextInfoTest {

        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.internalConfigurationAnnotationProcessorobject = org.springframework.context.annotation.ConfigurationClassPostProcessor@615091b8
name = org.springframework.context.annotation.internalAutowiredAnnotationProcessorobject = org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@4fce136b
name = org.springframework.context.annotation.internalCommonAnnotationProcessorobject = org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@4aa83f4f
name = org.springframework.context.event.internalEventListenerProcessorobject = org.springframework.context.event.EventListenerMethodProcessor@43b6123e
name = org.springframework.context.event.internalEventListenerFactoryobject = org.springframework.context.event.DefaultEventListenerFactory@260e86a1
name = appConfigobject = hello.core.AppConfig$$EnhancerBySpringCGLIB$$ef80b738@19648c40
name = memberServiceobject = hello.core.member.MemberServiceImpl@12bd8a64
name = orderServiceobject = hello.core.order.OrderServiceImpl@61e94def
name = memberRepositoryobject = hello.core.member.MemoryMemberRepository@3300f4fd
name = discountPolicyobject = hello.core.discount.RateDiscountPolicy@ebb6851

 

2. 너무 많은데..? 내가 등록한 빈 만 보고싶다.


  • ac.getBeanDefinition()을 활용하자.
  • BeanDefinition 중 ROLE_APPLICATION은 개발자 또는 외부 라이브러리에 의해 등록된 빈의 ROLE을 나타낸다.
@Test
@DisplayName("애플리케이션 빈 출력하기")
void findApplicationBean(){
  String[] beanDefinitionNames = ac.getBeanDefinitionNames();

        for (String beanDefinitionName : beanDefinitionNames) {

        BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);

        //Role_Application : 개발자, 외부라이브러리가 등록한빈.
        if(beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION){
          Object bean = ac.getBean(beanDefinitionName);
          System.out.println("name = " + beanDefinitionName + "object = " + bean);
        }

    }
}
  • 내가 등록한 빈 을 볼 수 있다.
name = appConfigobject = hello.core.AppConfig$$EnhancerBySpringCGLIB$$ef80b738@615091b8
name = memberServiceobject = hello.core.member.MemberServiceImpl@4fce136b
name = orderServiceobject = hello.core.order.OrderServiceImpl@4aa83f4f
name = memberRepositoryobject = hello.core.member.MemoryMemberRepository@43b6123e
name = discountPolicyobject = hello.core.discount.RateDiscountPolicy@260e86a1

 

3. BEAN의 ROLE


  • ROLE_APPLICATION : 직접 등록한 애플리케이션 빈
  • ROLE_INFRASTRUCTURE : 스프링이 내부에서 사용하는 빈
Comments