선 조치 후 분석

[Spring] Spring Framework - 핵심 원리 (16) - 컨테이너에 등록된 모든 빈 조회 + 등록된 빈(Bean) 확인 법 + getBeanDefinitionNames() + getBean() + getBeanDefinition() + ROLE_APPLICATION + ROLE_INFRASTRUCTURE 본문

Framework/Spring Framework

[Spring] Spring Framework - 핵심 원리 (16) - 컨테이너에 등록된 모든 빈 조회 + 등록된 빈(Bean) 확인 법 + getBeanDefinitionNames() + getBean() + getBeanDefinition() + ROLE_APPLICATION + ROLE_INFRASTRUCTURE

JB1104 2022. 1. 24. 23:59
728x90
반응형
SMALL

컨테이너에 등록된 모든 빈 조회 + 등록된 빈(Bean) 확인 법 + getBeanDefinitionNames() + getBean() + getBeanDefinition() + ROLE_APPLICATION + ROLE_INFRASTRUCTURE


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

 

1. 빈(Bean)을 조회할 클래스 생성

public class ApplicationContextInfoTest {
	
	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
	@Test
	@DisplayName("모든 빈 출력하기")
	void findAllBean() { // 참고 : 'Junit5'부터는 'void' 앞에 'public' 생략 가능
		String[] beanDefinitionNames =  ac.getBeanDefinitionNames();
		// 이름을 꺼낸다.
		
		for(String beanDefinitionName : beanDefinitionNames) {
			Object bean = ac.getBean(beanDefinitionName);
			// 타입을 모르기 때문에 'Object'로 꺼내진다.
			System.out.println("name : " + beanDefinitionName + "object : " + bean);
		}
	}

 

'모든 빈(Bean)' 출력하기

  • 실행하면 '스프링' 등록된 '모든 빈(Bean)'정보를 출력할 수 있다.
  • 'getBeanDefinitionNames()' : 스프링에 등록된 모든 빈 이름을 조회한다.
  • 'getBean()' : 빈 이름으로 빈 객체(인스턴스)를 조회한다.

 

결과를 보면 총 10줄의 빈(Bean)을 확인할 수 있다.

아래 5줄'스프링' 내부적으로 확장하려고 쓰는 '빈(Bean)'들이다.

name : org.springframework.context.annotation.internalConfigurationAnnotationProcessorobject : org.springframework.context.annotation.ConfigurationClassPostProcessor@11eadcba
name : org.springframework.context.annotation.internalAutowiredAnnotationProcessorobject : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor@4721d212
name : org.springframework.context.annotation.internalCommonAnnotationProcessorobject : org.springframework.context.annotation.CommonAnnotationBeanPostProcessor@1b065145
name : org.springframework.context.event.internalEventListenerProcessorobject : org.springframework.context.event.EventListenerMethodProcessor@45cff11c
name : org.springframework.context.event.internalEventListenerFactoryobject : org.springframework.context.event.DefaultEventListenerFactory@207ea13

 

아래 5줄직접 등록한 '빈(Bean)'이다.

name : appConfigobject : hello.core.AppConfig$$EnhancerBySpringCGLIB$$b307e9d0@4bff1903
name : memberServiceobject : hello.core.member.MemberServiceImpl@62dae540
name : memberRepositoryobject : hello.core.member.MemoryMemberRepository@5827af16
name : orderServiceobject : hello.core.order.OrderServiceImpl@654d8173
name : discountPolicyobject : hello.core.discount.FixDiscountPolicy@56c9bbd8

 

만약에 직접 등록한 '빈(Bean)'들만 보고 싶다면, 아래처럼 'for문'과 'if문'을 사용해서 코드를 작성해보자.

	@Test
	@DisplayName("애플리케이션 빈 출력하기")
	void findApplicationBean() { // 참고 : 'Junit5'부터는 'void' 앞에 'public' 생략 가능
		String[] beanDefinitionNames =  ac.getBeanDefinitionNames();
		
		for(String beanDefinitionName : beanDefinitionNames) {
			BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);
			
			if(beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
				Object bean = ac.getBean(beanDefinitionName);
				// 타입을 모르기 때문에 'Object'로 꺼내진다.
				System.out.println("name : " + beanDefinitionName + "object : " + bean);

'getBeanDefinition()' : 빈(Bean)에 대한 'meta data' 정보들을 반환한다.

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

 

 

즉, 반환한 정보가 'ROLE_APPLICATION '와 같다면, 해당하는 '빈(Bean)'들만 조회한다.

 

 

결과를 확인해보면 아래처럼 나오는 것을 볼 수 있다.

(반대로 자체적으로 확장한 '빈(Bean)'을 보고 싶다면 'ROLE_INFRASTRUCTURE'을 사용하면 된다.)

name : appConfigobject : hello.core.AppConfig$$EnhancerBySpringCGLIB$$b307e9d0@11eadcba
name : memberServiceobject : hello.core.member.MemberServiceImpl@4721d212
name : memberRepositoryobject : hello.core.member.MemoryMemberRepository@1b065145
name : orderServiceobject : hello.core.order.OrderServiceImpl@45cff11c
name : discountPolicyobject : hello.core.discount.FixDiscountPolicy@207ea13

 

728x90
반응형
LIST