반응형
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
@Test
@DisplayName("애플리케이션 빈 출력하기")
void findApplicationBean() {
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);
if (beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
Object bean = ac.getBean(beanDefinitionName);
System.out.println("bean = " + beanDefinitionName + " object = " + bean);
}
}
}
getBeanDefinitionNames() : 스프링에 등록된 모든 빈 이름 조회
getBean() : 빈 이름으로 빈 객체 조회
getRole() : 스프링 내부에서 사용하는 빈 구분
ROLE_APPLICATION : 사용자가 직접 등록한 빈
ROLE_INFRASTRUCTURE : 스프링이 내부에서 사용하는 모든 빈
실행 결과
bean = appConfig object = hello.core.AppConfig$$SpringCGLIB$$0@b2c5e07
bean = memberService object = hello.core.member.MemberServiceImpl@5812f68b
bean = memberRepository object = hello.core.member.MemoryMemberRepository@4426bff1
bean = orderService object = hello.core.order.OrderServiceImpl@3c7c886c
bean = discountPolicy object = hello.core.discount.RateDiscountPolicy@55493582
Process finished with exit code 0
Appconfig 클래스에 있는 MemberService, MemoryMemberRepository, OrderServiceImpl, RateDiscountPolicy
가 bean 객체로 저장되어서 조회되는 모습을 볼 수 있다.
출처 : 인프런 - 스프링 핵심 원리 (기본편)
반응형
'Spring > Spring' 카테고리의 다른 글
[Spring] BeanFacory와 ApplicationContext (0) | 2024.08.30 |
---|---|
[Spring] 빈 조회하기 - 상속 관계 (1) | 2024.08.30 |
[Spring] 스프링 컨테이너 (0) | 2024.08.29 |
[Spring] API(JSON 방식)와 @ResponseBody (0) | 2024.08.24 |
[Spring] 정적 컨텐츠, MVC, 템플릿 엔진 (0) | 2024.08.24 |