반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 | 29 |
30 | 31 |
Tags
- DI
- DIP
- 스프링 부트 기본
- 스프링부트
- 스프링 빈
- @Configuration
- assertThat
- Javascript
- 싱글톤
- resultMap
- thymeleaf
- sqld
- db
- assertThrows
- 스프링 프레임워크
- JPA
- 필드 주입
- SQL
- 생성자 주입
- jdbc
- 스프링 부트
- Effective Java
- 스프링 컨테이너
- 스프링 부트 입문
- 스프링
- java
- spring
- springboot
- mybatis
- kafka
Archives
- Today
- Total
선 조치 후 분석
[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:59728x90
반응형
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