Skip to content

Commit

Permalink
2022-12-26-Mon-17:35
Browse files Browse the repository at this point in the history
  • Loading branch information
liberalwig committed Dec 26, 2022
1 parent 412c498 commit c1f628b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/test/java/hello/core/beanfind/ApplicationContextInfoTest.java
Original file line number Diff line number Diff line change
@@ -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
}
}
}
}

0 comments on commit c1f628b

Please sign in to comment.