🧐강의🧐
https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%95%B5%EC%8B%AC-%EC%9B%90%EB%A6%AC-%EA%B8%B0%EB%B3%B8%ED%8E%B8/dashboard
✨ 스프링 빈 조회 - 기본
☑️ 스프링 컨테이너에서 스프링 빈을 찾는 가장 기본적인 조회 방법
· ac.getBean(빈 이름, 타입)
· ac.getBean(타입)
⚠️ 주의 ⚠️
조회 대상의 스프링 빈이 없으면 예외 발생
- NoSuchBeanDefinitionException : No bean named 'xxxx' available
· test/java/hello.core/beanfind/ApplicationContextBasicFindTest
1. 스프링 컨테이너 구성 정보 가져오기
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
2-1. 빈 이름으로 조회하기
MemberService memberService = ac.getBean("memberService", MemberService.class);
(+) 검증 : Assertions (orgs.junit)
2-2. 빈 이름 없이 타입으로만 조회하기
MemberService memberService = ac.getBean(MemberService.class);
2-3. 구체 타입으로 조회하기
→ 인터페이스의 구현체로 직접 조회 : 좋지 않은 방법
MemberService memberService = ac.getBean("memberService", MemberServiceImpl.class);
3. 실패 테스트 만들어보기
assertThrows(NoSuchBeanDefinitionException.class,
() -> ac.getBean("xxxx", MemberService.class));
(+) 검증하는 방법
→ Assertions (org.junit) 사용
assertThat(memberService).isInstanceOf(MemberServiceImpl.class);
✨ 스프링 빈 조회 - 동일한 타입이 둘 이상
☑️ 타입으로 빈 조회 시, 같은 타입의 스프링 빈이 둘 이상이면 오류 발생 → 빈 이름 지정
· ac.getBeansOfType() : 해당 타입의 모든 빈 조회 가능
· test/java/hello.core/beanfind/ApplicationContextSameBeanFindTest
1. static class 선언하기
→ ApplicationContextSameBeanFindTest 내에서만 사용 가능한 클래스
@Configuration
static class SameBeanConfig {
@Bean
public MemberRepository memberRepository1() {
return new MemoryMemberRepository();
}
@Bean
public MemberRepository memberRepository2() {
return new MemoryMemberRepository();
}
}
2. 스프링 컨테이너 구성 정보 불러오기
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(SameBeanConfig.class);
3. 빈 타입으로 조회 시, 같은 타입이 둘 이상이면 중복 오류 발생 확인하기
assertThrows(NoUniqueBeanDefinitionException.class,
() -> ac.getBean(MemberRepository.class));
4-1. 빈 타입으로 조회 시, 같은 타입이 둘 이상이면 빈 이름 지정하기
MemberRepository memberRepository = ac.getBean("memberRepository1", MemberRepository.class);
(+) 검증 : assertThat(memberRepository).isInstanceOf(MemberRepository.class)
4-2. 특정 타입의 빈 모두 조회하기
Map<String, MemberRepository> beansOfType = ac.getBeansOfType(MemberRepository.class);
for (String key : beansOfType.keySet()) {
System.out.println("key = " + key + " value = " + beansOfType.get(key));
}
System.out.println("beansOfType = " + beansOfType);
(+) 검증 : assertThat(beansOfType.size()).isEqualTo(2)
✨ 스프링 빈 조회 - 상속 관계
☑️ 부모 타입의 빈 조회 시, 자식 타입도 함께 조회 → Object 타입으로 빈 조회 시, 모든 스프링 빈 조회 가능
· Object 타입 : 모든 자바 객체의 최고 부모
· test/java/hello.core/beanfind/ApplicationContextExtendsFindTest
1. static class 선언하기
@Configuration
static class TestConfig {
@Bean
public DiscountPolicy rateDiscountPolicy() {
return new RateDiscountPolicy();
}
@Bean
public DiscountPolicy fixDiscountPolicy() {
return new FixDiscountPolicy();
}
}
2. 스프링 컨테이너 구성 정보 불러오기
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(TestConfig.class);
3. 부모 타입으로 조회 시, 자식이 둘 이상이면 중복 오류 발생 확인하기
assertThrows(NoUniqueBeanDefinitionException.class,
() -> ac.getBean(DiscountPolicy.class));
4-1. 부모 타입으로 조회 시, 자식이 둘 이상이면 빈 이름 지정하기
DiscountPolicy rateDiscountPolicy = ac.getBean("rateDiscountPolicy", DiscountPolicy.class);
(+) 검증 : assertThat(rateDiscountPolicy).isInstanceOf(RateDiscountPolicy.class)
4-2. 특정 하위 타입으로 빈 조회하기
RateDiscountPolicy bean = ac.getBean(RateDiscountPolicy.class);
(+) 검증 : assertThat(bean).isInstanceOf(RateDiscountPolicy.class);
4-3. 부모 타입으로 빈 모두 조회하기
Map<String, DiscountPolicy> beansOfType = ac.getBeansOfType(DiscountPolicy.class);
for (String key : beansOfType.keySet()) {
System.out.println("key = " + key + "value = " + beansOfType.get(key));
}
(+) 검증 : assertThat(beansOfType.size()).isEqualTo(2);
4-4. Object 타입으로 빈 모두 조회하기
Map<String, Object> beansOfType = ac.getBeansOfType(Object.class);
for (String key : beansOfType.keySet()) {
System.out.println("key = " + key + "value = " + beansOfType.get(key));
}
'Spring > 스프링 핵심 원리 - 기본편' 카테고리의 다른 글
[스프링 컨테이너와 스프링 빈] 다양한 설정 형식 지원 - 자바 코드, XML (0) | 2023.11.24 |
---|---|
[스프링 컨테이너와 스프링 빈] BeanFactory 와 ApplicationContext (0) | 2023.11.23 |
[스프링 컨테이너와 스프링 빈] 컨테이너에 등록된 모든 빈 조회 (0) | 2023.11.21 |
[스프링 컨테이너와 스프링 빈] 스프링 컨테이너 생성 (0) | 2023.11.20 |
[이해2 - 객체 지향 원리 적용] 스프링으로 전환하기 (0) | 2023.11.19 |