모든 빈 출력하기
예제) 테스트 코드
package hello.core.beanFind;
import hello.core.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("beanDefinition = " + beanDefinitionName +
" object = " + bean);
}
}
}
}
- ac.getBeanDefinitionNames( ) : 스프링에 등록된 모든 빈 이름을 조회한다.
- ac.getBean("빈 이름" ) : 빈 이름으로 빈 객체(인스턴스)를 조회한다.
- ROLE_APPLICAITON : 일반적으로 사용자가 정의한 빈
- ROLE_INFRASTRUCTURE : 스프링이 내부에서 사용하는 빈
'Spring' 카테고리의 다른 글
Spring- 스프링 빈 메타 정보 설정(BeanDefinition) (0) | 2023.01.19 |
---|---|
Spring- BeanFactory와 ApplicationContext (0) | 2023.01.19 |
Spring- 스프링 컨테이너로 등록하기(2) (0) | 2023.01.17 |
Spring- 스프링 컨테이너로 등록하기(1) (0) | 2023.01.17 |
Spring- IoC, DI 컨테이너 (0) | 2023.01.17 |