본문 바로가기
강의 실습/스프링 핵심 원리 - 기본편

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

by jint 2026. 4. 8.

스프링 컨테이너에 실제 스프링 빈들이 잘 등록 되었는지 확인

- src/test/java/hello/core/beanfind/ApplicationContextInfoTest.java

package hello.core.beanfind;

import hello.core.AppConfig;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import static org.assertj.core.api.Assertions.assertThat;

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);
        }
    }

    @Test
    @DisplayName("애플리케이션 빈 출력하기")
    void findApplicationBean() {
        String[] beanDefinitionNames = ac.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName); // bean 에 대한 metadata 정보

            // Role ROLE_APPLICATION : 직접 등록한 애플리케이션 빈
            // Role ROLE_INFRASTRUCTURE : 스프링이 내부에서 사용하는 빈
            if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
                Object bean = ac.getBean(beanDefinitionName);
                System.out.println("name = " + beanDefinitionName + ", object = " + bean);
            }
        }
    }

}


1) 모든 빈 출력
스프링에 등록된 모든 빈 정보 출력

- 콘솔

name = org.springframework.context.annotation.internalConfigurationAnnotationProcessor, object = org.springframework.context.annotation.ConfigurationClassPostProcessor@74294c1a
name = org.springframework.context.annotation.internalAutowiredAnnotationProcessor, object = org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@618ad2aa
name = org.springframework.context.annotation.internalCommonAnnotationProcessor, object = org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@1aa6e3c0
name = org.springframework.context.event.internalEventListenerProcessor, object = org.springframework.context.event.EventListenerMethodProcessor@3531f3ca
name = org.springframework.context.event.internalEventListenerFactory, object = org.springframework.context.event.DefaultEventListenerFactory@7fcf294e
name = appConfig, object = hello.core.AppConfig$$SpringCGLIB$$0@4867ab9f
name = memberService, object = hello.core.member.MemberServiceImpl@65f2f9b0
name = orderService, object = hello.core.order.OrderServiceImpl@5fe7f967
name = memberRepository, object = hello.core.member.MemoryMemberRepository@59e43e8c
name = discountPolicy, object = hello.core.discount.RateDiscountPolicy@2caa5d7c


ApplicationContext.getBeanDefinitionNames() : 스프링에 등록된 모든 빈 이름 조회
ApplicationContext.getBean() : 빈 이름, 타입으로 빈 객체(인스턴스) 조회

2) 애플리케이션 빈 출력
스프링이 내부에서 사용하는 빈은 제외하고, 직접 등록한 빈만 출력

- 콘솔

name = appConfig, object = hello.core.AppConfig$$SpringCGLIB$$0@74294c1a
name = memberService, object = hello.core.member.MemberServiceImpl@618ad2aa
name = orderService, object = hello.core.order.OrderServiceImpl@1aa6e3c0
name = memberRepository, object = hello.core.member.MemoryMemberRepository@3531f3ca
name = discountPolicy, object = hello.core.discount.RateDiscountPolicy@7fcf294e


BeanDefinition.getRole() : 어떤 역할의 빈인지 조회
BeanDefinition.ROLE_APPLICATION : 직접 등록한 (사용자가 정의한) 애플리케이션 빈
BeanDefinition.ROLE_INFRASTRUCTURE : 스프링이 내부에서 사용하는 빈


참고링크 : https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%95%B5%EC%8B%AC-%EC%9B%90%EB%A6%AC-%EA%B8%B0%EB%B3%B8%ED%8E%B8?cid=325969

 

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

현재 평점 5.0점 수강생 49,534명인 강의를 만나보세요. 스프링 입문자가 예제를 만들어가면서 스프링의 핵심 원리를 이해하고, 스프링 기본기를 확실히 다질 수 있습니다. 스프링 기본 기능, 스프

www.inflearn.com

댓글