[Spring-기본] 스프링 컨테이너와 스프링 빈 - 3

DANI·2023년 11월 24일

Spring[김영한T]

목록 보기
22/31
post-thumbnail

📑 스프링 빈 설정 메타 정보 - BeanDefinition


스프링이 다양한 설정 형식을 지원할 수 있는 이유 중 하나엔 BeanDefinition이라는 추상화가 있다.

👉 XML을 읽어서 BeanDefinition 을 생성한다
👉 자바 코드를 읽어서 BeanDefinition을 생성한다

👉 즉, 스프링 컨테이너는 오직 BeanDefinition만 있으면 된다!


간단히 말해 역할구현으로 나눈 것이다.



각 설정파일은 BeanDefinition을 생성한다.




🔍 BeanDefinition 살펴보기

  • BeanClassName : 생성할 빈의 클래스 명

  • factoryBeanName : 팩토리 역할의 빈을 사용할 경우 ex) AppConfig

  • factoryMethodName : 빈을 생성할 팩토리 메서드 ex) memberService

  • lazyInit : 스프링 컨테이너를 생성할 때 빈을 생성하는 것이 아니라, 실제 빈을 사용할 때 까지 최대한 생성을 지연처리 하는 지 여부

  • lnitMethodName : 빈을 생성하고, 의존관계를 적용한 뒤에 호출되는 초기화 메서드 명

  • DestoryMethodName : 빈의 생명주기가 끝나서 제거하기 직전에 호출되는 메서드 명

  • Constructor arguments, Properties : 의존관계 주입에서 사용한다.




💻 테스트파일로 beanDefinition에 대해 알아보자

💾 BeanDefinitionTest 파일생성

package hello.core;

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 BeanDefinitionTest {


  AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

  @Test
  @DisplayName("빈 설정 메타정보 확인하기")
  void findApplicationBean(){
      String[] beanDefinirtionNames = ac.getBeanDefinitionNames();
      for(String beans : beanDefinirtionNames){
          BeanDefinition beanDefinition = ac.getBeanDefinition(beans);
          if(beanDefinition.getRole()==BeanDefinition.ROLE_APPLICATION){
              System.out.println("beanDefinitionName : " + beans + ", beanDefinition : "+beanDefinition);
          }
      }
  }
}

🔵 실행 결과

beanDefinitionName : appConfig, beanDefinition : Generic bean: class [hello.core.AppConfig$$SpringCGLIB$$0]; scope=singleton; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodNames=null; destroyMethodNames=null
beanDefinitionName : orderService, beanDefinition : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfig; factoryMethodName=orderService; initMethodNames=null; destroyMethodNames=[(inferred)]; defined in hello.core.AppConfig
beanDefinitionName : memberRepository, beanDefinition : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfig; factoryMethodName=memberRepository; initMethodNames=null; destroyMethodNames=[(inferred)]; defined in hello.core.AppConfig
beanDefinitionName : discountPolicy, beanDefinition : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfig; factoryMethodName=discountPolicy; initMethodNames=null; destroyMethodNames=[(inferred)]; defined in hello.core.AppConfig
beanDefinitionName : memberService, beanDefinition : Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=appConfig; factoryMethodName=memberService; initMethodNames=null; destroyMethodNames=[(inferred)]; defined in hello.core.AppConfig

Process finished with exit code 0


beanDefinition 의 정보에 대해 알 수 있다!

0개의 댓글