<aside> 💡 3주차 정리

</aside>

스프링 컨테이너는 어떤 식으로 생성될까?

//스프링 컨테이너 생성
ApplicationContext applicationContext = 
						new AnnotationConfigApplicationContext (AppConfig.class);

01 스프링 컨테이너 생성과정

  1. 스프링 컨테이너 생성

    Untitled

  2. 스프링 빈 등록

    Untitled

  3. 스프링 빈 의존관계 설정 - 준비

    Untitled

  4. 스프링 빈 의존관계 설정 - 완료

    Untitled

이제 스프링 컨테이너에서 데이터를 조회해보자.

02 스프링 빈 조회 - 기본

실제로 스프링 컨테이너에 스프링 빈들이 잘 등록되어있는지 확인해보자.

package core.order.beanfind;

import core.order.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;

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

    @Test
    @DisplayName("애플리케이션 빈 출력하기")
//우리가 만든 빈만 출력
    void findApplicationBean() {
        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);
            }
        }
    }
}