You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SameBeanConfig 클래스에서 memberRepository1, memberRepository2 두개 같은 타입 빈을 만들고 조회 하면 아래와 같이 NoUniqueBeanDefinitionException 중복 빈 오류가 빌셍 힌디/
No qualifying bean of type 'hello.core.member.MemberRepository' available: expected single matching bean but found 2: memberRepository1,memberRepository2
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'hello.core.member.MemberRepository' available: expected single matching bean but found 2: memberRepository1,memberRepository2
예제코드
packagehello.core.beanfind;
importhello.core.member.MemberRepository;
importhello.core.member.MemoryMemberRepository;
importorg.junit.jupiter.api.DisplayName;
importorg.junit.jupiter.api.Test;
importorg.springframework.beans.factory.NoUniqueBeanDefinitionException;
importorg.springframework.context.annotation.AnnotationConfigApplicationContext;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importjava.util.Map;
importstaticorg.assertj.core.api.Assertions.assertThat;
importstaticorg.junit.jupiter.api.Assertions.assertThrows;
publicclassApplicationContextSameBeanFindTest {
AnnotationConfigApplicationContextac = newAnnotationConfigApplicationContext(SameBeanConfig.class);
@Test@DisplayName("타입으로 조회시 같은 타입이 둘 이상 있으면, 중복 오류가 발생한다.")
voidfindBeanByTypeDuplicate(){
// MemberRepository bean = ac.getBean(MemberRepository.class);assertThrows(NoUniqueBeanDefinitionException.class,
() -> ac.getBean(MemberRepository.class));
}
@Test@DisplayName("타입으로 조회시 같은 타입이 둘 이상 있으면, 빈 이름을 지정하면 된다")
voidfindBeanByName() {
MemberRepositorymemberRepository = ac.getBean("memberRepository1", MemberRepository.class);
assertThat(memberRepository).isInstanceOf(MemberRepository.class);
}
@Test@DisplayName("특정 타입을 모두 조회하기")
voidfindAllBeanByType() {
Map<String, MemberRepository> beansOfType = ac.getBeansOfType(MemberRepository.class);
for (Stringkey : beansOfType.keySet()){
System.out.println("key = " + key + " value = " + beansOfType.get(key));
}
System.out.println("beansOfType = " + beansOfType);
assertThat(beansOfType.size()).isEqualTo(2);
}
@ConfigurationstaticclassSameBeanConfig {
@BeanpublicMemberRepositorymemberRepository1() {
returnnewMemoryMemberRepository();
}
@BeanpublicMemberRepositorymemberRepository2() {
returnnewMemoryMemberRepository();
}
}
}
findBeanByTypeDuplicate: 같은 타입이 둘 이상 있으면, 중복 오류가 생김
findBeanByName: 중복 오류시 이름 지정해서 인스턴스 가져오기
findAllBeanByType: 특정 타입 빈들을 모두 조회 한다.
스프링 빈 조회 - 상속 관계
부모 타입으로 조회하면, 자식 타입도 함꼐 조회 한다.
모든 자바 객체의 최고 부모인 Object 타입으로 조회하면, 모든 스피링 빈을 조회 한다.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
스프링 컨테이너와 스프링 빈
스프링 컨테이너 생성
ApplicationContext: 스프링 컨테이너이고 인터페이스이다.AppConfig를 사용했던 방식이 애노테이션 기반의 자바 설정 클래스로 스프링 컨테이너를 만든 것이다.스프링 컨테이너의 생성 과정
1. 스프링 컨테이너 생성
new AnnotationConfigApplicationContext(AppConfig.class):AppConfig에 있는 컨테이너 생성2. 스프링 빈 등록
@Bean(name="memberService2"))3. 스프링 빈 의존관계 설정 - 준비
4. 스프링 빈 의존관계 설정 - 완료
컨테이너에 등록된 모든 빈 조회
findAllBean())ac.getBeanDefinitionNames();: 스프링에 등록된 모든 빈 이름을 조회한다.ac.getBean(): 빈 이름으로 빈 객체(인스턴스)를 조회한다.findApplicationBean())getRole()로 구분할 수 있다.스프링 빈 조회 - 기본
NoSuchBeanDefinitionException: No bean named 'xxxxx' available예제코드
스프링 빈 조회 - 동일한 타입이 둘 이상
.getBeansOfType()을 사용하며 해당 타입의 모든 빈을 조회할 수 있다.같은 타입 스프링 빈 생성
SameBeanConfig클래스에서memberRepository1,memberRepository2두개 같은 타입 빈을 만들고 조회 하면 아래와 같이NoUniqueBeanDefinitionException중복 빈 오류가 빌셍 힌디/예제코드
findBeanByTypeDuplicate: 같은 타입이 둘 이상 있으면, 중복 오류가 생김findBeanByName: 중복 오류시 이름 지정해서 인스턴스 가져오기findAllBeanByType: 특정 타입 빈들을 모두 조회 한다.스프링 빈 조회 - 상속 관계
Object타입으로 조회하면, 모든 스피링 빈을 조회 한다.예제 코드
BeanFactory와 ApplicationContext
BeanFactory와
getBean()으로 빈을 가져온다.ApplicationContext
ApplicationContext가 제공하는 부가기능
정리
다양한 설정 형식 지원 - 자바 코드, XML
애노테이션 기반 자바 코드 설정 사용
new AnnotationConfigApplicationContext(AppConfig.class)AnnotationConfigApplicationContext클래스 사용하면서 자바 코드로된 설정 정보를 넘기면 된다.XML 설정 사용
GenericXmlApplicationContext를 사용하면 xml 설정 파일을 넘길수 있다.자바 코드
xml 스프링 빈 설정
src/main/resources/appConfig.xmlAppConfig.java설정 정보를 비교해보면 거의 비슷 하다.스프링 빈 설정 메타 정보 - BeanDefinition
BeanDefinition이라는 추상화에서 이루어진다.BeanDefinition에서 역할과 구현을 개념적으로 나눈것이다.BeanDefinition을 빈 설정 메타정보라 한다.,@Bean,<bean>당 각각 하나씩 메타 정보가 생성된다.AnnotationConfigApplicationContext는AnnotatedBeanDefinitionReader를 사용해서AppConfig.class를 읽고BeanDefinition을 생성한다.GenericXmlApplicationContext는XmlBeanDefinitionReader를 사용해서appConfig.xml설정 정보를 읽고BeanDefinition을 생성한다.BeanDefinition을 생성하면 된다.BeanDefinition 살펴보기
정리
All reactions