From c1f628bda31b3c3891cd3d6516867e094c5aaa8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=8A=B9=EC=9D=80?= Date: Mon, 26 Dec 2022 17:35:20 +0900 Subject: [PATCH] 2022-12-26-Mon-17:35 --- .../beanfind/ApplicationContextInfoTest.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 src/test/java/hello/core/beanfind/ApplicationContextInfoTest.java diff --git a/src/test/java/hello/core/beanfind/ApplicationContextInfoTest.java b/src/test/java/hello/core/beanfind/ApplicationContextInfoTest.java new file mode 100644 index 0000000..eb86605 --- /dev/null +++ b/src/test/java/hello/core/beanfind/ApplicationContextInfoTest.java @@ -0,0 +1,36 @@ +package hello.core.beanfind; + +import hello.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("모든 Bean 출력") + void findAllBean() { + String[] beanDefinitionNames = ac.getBeanDefinitionNames(); + for (String beanDefinitionName : beanDefinitionNames) { + Object bean = ac.getBean(beanDefinitionName); + System.out.println("name = " + beanDefinitionName + " object = " + bean); // bean 이 key, soutv 부분이 value + } + } + @Test + @DisplayName("Application Bean 출력") // ROLE_APPLICATION: 직접 등록한 애플리케이션 Bean + void findApplicationBean() { + String[] beanDefinitionNames = ac.getBeanDefinitionNames(); + for (String beanDefinitionName : beanDefinitionNames) { + BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName); + + // cf) ROLE_INFRASTRUCTURE: 스프링이 내부에서 사용하는 Bean + if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) { + Object bean = ac.getBean(beanDefinitionName); + System.out.println("name = " + beanDefinitionName + " object = " + bean); // bean 이 key, soutv 부분이 value + } + } + } +}