티스토리 뷰
빈 이름으로 조회
@Test
void findBean() {
// 이름으로 조회
Object bean = ac.getBean("memberRepository", MemberRepository.class);
// 타입으로 조회
Object bean2 = ac.getBean(MemberRepository.class);
}
- 이름이 같은 빈이 두개 이상있거나, 동일한 타입이 두 개 이상이면 덮어 씌어지거나 충돌
- 반드시 빈의 이름은 유니크하게 짓자
- 상속 관계의 빈 조회시, 조회 한 빈의 자손은 모두 딸려옴
- 따라서 조회한 빈의 자식이 둘 이상이면 중복 오류남 (NoUniqueBeanDefinitionException)
- 따라서 빈의 이름으로 조회하면 된다
팩토리에 등록된 모든 빈 이름 조회
AnnotationConfigApplicationContext 에 등록된 모든 빈들의 이름 리턴
@SpringBootTest
class SpringmvcApplicationTests {
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
@Test
void findApplicationBean() {
String[] beanDefinitionNames = ac.getBeanDefinitionNames();
for (String beanDefinitionName : beanDefinitionNames) {
BeanDefinition beanDefinition = ac.getBeanDefinition(beanDefinitionName);
// ROLE_APPLICATION : 내가 정의한 빈
// ROLE_INFRASTRUCTURE : 스프링이 등록한 빈
if(beanDefinition.getRole() == BeanDefinition.ROLE_APPLICATION) {
Object bean = ac.getBean(beanDefinitionName);
System.out.println(bean);
}
}
}
}
BeanFactory 와 ApplicationContext
- BeanFactory
- 스프링 컨테이너의 최상위 인터페이스
- 스프링 빈 관리, 조회 역할 (getBean())
- ApplicationContext
- BeanFactory 를 상속받음
- MessageSource
- 국제화 기능
- EnvironmentCapable
- 환경 변수, 로컬, 개발, 운영등을 구분해서 처리
- ResourceLoader
- 편리한 리소스 조회
- 파일, 클래스패스, 외부 등에서 리소스 편리하게 조회
BeanDefinition
- 스프링 빈의 메타 정보
- 어떤 형식이든 (class든 xml 이든) 빈의 정보는 추상화된 BeanDefinition 이 된다.
- 스프링은 다양한 형태의 설정 정보를 BeanDefinition 으로 추상화 해서 사용한다
'CS 정리 > Spring' 카테고리의 다른 글
의존관계 주입 방법들 (0) | 2023.11.17 |
---|---|
컴포넌트 스캔 (0) | 2023.11.17 |
싱글톤 컨테이너 (0) | 2023.11.16 |
Spring, 단계적으로 OCP가 어떻게 지켜지는지 (0) | 2023.11.16 |
객체지향 5원칙 SOLID (0) | 2023.10.28 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- Python
- 조합
- greedy
- Tree
- two pointer
- DP
- priority queue
- 자료구조
- BFS
- dfs
- Implementation
- Unity
- CSS
- Spring
- C++
- Kruskal
- Stack
- 이분탐색
- floyd warshall
- graph
- Brute Force
- Dijkstra
- C
- permutation
- recursion
- 재귀
- MVC
- db
- back tracking
- binary search
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함