본문 바로가기
Spring/Spring core basic

[Spring core basic] 18 - 컨테이너에 등록된 모든 빈 조회

by Kloong 2022. 5. 2.

참고

더보기

Spring core basic 시리즈는 김영한 님의 "스프링 핵심 원리 - 기본편" 강의를 정리한 글입니다. 글에 첨부된 사진은 해당 강의의 강의 자료에서 캡쳐한 것입니다. 제 Github에만 올려뒀다가, 정보 공유와 강의 홍보(?)를 위해 블로그에도 업로드합니다. 마크다운을 잘 쓰지 못해서 가독성이 조금 떨어지는 점 양해 바랍니다.

 

 

스프링 핵심 원리 - 기본편 - 인프런 | 강의

스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다., - 강의 소개 | 인프런...

www.inflearn.com

컨테이너에 등록된 모든 빈 조회

모든 빈 출력하기

ApplicationContextInfoTest.java

package com.kloong.corebasic1.findbean;

//import 생략

public class ApplicationContextInfoTest {
    AnnotationConfigApplicationContext ac = new
    AnnotationConfigApplicationContext(AppConfig.class);

    @Test
    @DisplayName("모든 빈 출력하기")
    void findAllBeans() {
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            Object bean = ac.getBean(beanDefinitionName);
            System.out.println(
            "name = " + beanDefinitionName + " object = " + bean);
        }
    }
}

실행하면 스프링에 등록된 모든 빈 정보를 출력할 수 있다.

  • ac.getBeanDefinitionNames(): 스프링에 등록된 모든 빈 이름을 조회한다.
  • ac.getBean(): 빈 이름으로 객체를 조회한다.

실행 결과

/* 이전 로그 생략 */
name = org.springframework.context.annotation.internalConfigurationAnnotationProcessor object = org.springframework.context.annotation.ConfigurationClassPostProcessor@50dfbc58
name = org.springframework.context.annotation.internalAutowiredAnnotationProcessor object = org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@4416d64f
name = org.springframework.context.annotation.internalCommonAnnotationProcessor object = org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@6bf08014
name = org.springframework.context.event.internalEventListenerProcessor object = org.springframework.context.event.EventListenerMethodProcessor@5e3d57c7
name = org.springframework.context.event.internalEventListenerFactory object = org.springframework.context.event.DefaultEventListenerFactory@732d0d24
name = appConfig object = com.kloong.corebasic1.AppConfig$$EnhancerBySpringCGLIB$$af9199@1fb19a0
name = memberRepository object = com.kloong.corebasic1.member.MemoryMemberRepository@6ee4d9ab
name = discountPolicy object = com.kloong.corebasic1.discount.RateDiscountPolicy@5a5338df
name = memberService object = com.kloong.corebasic1.member.MemberServiceImpl@418c5a9c
name = orderService object = com.kloong.corebasic1.order.OrderServiceImpl@18e36d14

AppConfig를 통해 등록된 빈 외에도 Spring에서 자체적으로 등록한 빈들의 정보가 나타는 것을 확인할 수 있다.

애플리케이션 빈 출력하기

스프링이 내부에서 사용하기 위해 등록한 빈은 제외하고, AppConfig를 통해 등록한 빈만 출력해보자.

getRole() 메소드를 사용해서 스프링 내부에서 사용하는 빈과 사용자가 등록한 빈을 구분할 수 있다.

ApplicationContextInfoTest.java

package com.kloong.corebasic1.findbean;

//import 생ㄹ햑

public class ApplicationContextInfoTest {
    AnnotationConfigApplicationContext ac =
    new AnnotationConfigApplicationContext(AppConfig.class);

    @Test
    @DisplayName("애플리케이션 빈 출력하기")
    void findApplicationBeans() {
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            BeanDefinition beanDefinition =
            ac.getBeanDefinition(beanDefinitionName);

            //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 = com.kloong.corebasic1.AppConfig$$EnhancerBySpringCGLIB$$af9199@1fb19a0
name = memberRepository object = com.kloong.corebasic1.member.MemoryMemberRepository@6ee4d9ab
name = discountPolicy object = com.kloong.corebasic1.discount.RateDiscountPolicy@5a5338df
name = memberService object = com.kloong.corebasic1.member.MemberServiceImpl@418c5a9c
name = orderService object = com.kloong.corebasic1.order.OrderServiceImpl@18e36d14

스프링 내부에서 사용하는 빈이 나타나지 않는다. 각 스프링 빈의 실제 객체가 무엇인지 확인할 수 있다.

댓글