인프런 커뮤니티 질문&답변

sin9801님의 프로필 이미지

작성한 질문수

스프링 핵심 원리 - 기본편

옵션 처리

NoUniqueBeanDefinitionException, UnsatisfiedDependencyException 질문

해결된 질문

24.06.24 22:48 작성

·

279

0

학습하는 분들께 도움이 되고, 더 좋은 답변을 드릴 수 있도록 질문전에 다음을 꼭 확인해주세요.

1. 강의 내용과 관련된 질문을 남겨주세요.
2. 인프런의 질문 게시판과 자주 하는 질문(링크)을 먼저 확인해주세요.
(자주 하는 질문 링크: https://bit.ly/3fX6ygx)
3. 질문 잘하기 메뉴얼(링크)을 먼저 읽어주세요.
(질문 잘하기 메뉴얼 링크: https://bit.ly/2UfeqCG)

질문 시에는 위 내용은 삭제하고 다음 내용을 남겨주세요.
=========================================
[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? (예/아니오)
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예/아니오)
3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예/아니오)

[질문 내용]
안녕하세요 김영한님의 스프링 강의를 듣던 도중 해결되지 않는 문제가 생겨 글을 남깁니다.

전체 Test를 돌리던 중에 CoreApplicationTests에서

java.lang.IllegalStateException: Failed to load ApplicationContext for [MergedContextConfiguration@2cc03cd1 testClass = hello.core.CoreApplicationTests, locations = [], classes = [hello.core.CoreApplication], contextInitializerClasses = [], activeProfiles = [], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@b91d8c4, org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@4fd4cae3, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2baa8d82, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@142eef62, org.springframework.boot.test.context.SpringBootTestAnnotation@4ec067d1], contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]

이라는 오류가 발생했습니다.

 

그래서 원인을 찾기 위해 로그를 읽어내려가던 중

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'memberServiceImpl' defined in file [/Users/sin-eunsu/study/core/out/production/classes/hello/core/member/MemberServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type 'hello.core.member.MemberRepository' available: expected single matching bean but found 2: memoryMemberRepository,memberRepository

 

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'hello.core.member.MemberRepository' available: expected single matching bean but found 2: memoryMemberRepository,memberRepository

 

UnsatisfiedDependencyException, NoUniqueBeanDefinitionException때문에 오류가 생겼다는 로그를 발견했습니다.

해당 오류를 해결하기 위해 memberServiceImpl과 orderServiceImpl에 있는 Component어노테이션을 비활성화 처리를 하면 CoreApplicationTests를 제외한 다른 테스트에서 오류가 생깁니다.

해당 로그들에 따르면 memoryMemberRepository, memberRepository 스프링 빈이 해당 테스트에서 2개가 발견되어 오류가 생겼다는 뜻같은데 어떻게하면 해결할 수 있나요?

스크린샷 2024-06-24 오후 10.44.34.png스크린샷 2024-06-24 오후 10.46.08.png

답변 1

0

인프런 AI 인턴님의 프로필 이미지

2024. 06. 24. 22:51

안녕하세요, 인프런 AI 인턴입니다.

sin9801님께서 겪고 계신 NoUniqueBeanDefinitionExceptionUnsatisfiedDependencyException 문제는 스프링 컨테이너가 특정 타입의 빈을 주입하려 할 때 예상했던 하나의 빈 대신 2개의 빈이 발견되어 발생하는 문제와 관련이 있습니다. 이 문제는 스프링이 어떤 빈을 주입해야 할지 결정할 수 없을 때 발생합니다. sin9801님의 경우 MemberRepository 타입에 대해 memoryMemberRepositorymemberRepository 두 가지 빈이 발견되었다고 합니다.

이와 유사한 문제와 그 해결 방법에 대해 과거의 질문에서 논의되었습니다. 아래 링크를 참고하시면 도움이 될 것 같습니다.

이 게시물에서는 비슷한 문제를 경험하신 다른 학습자가 memoryMemberRepositorymemberRepository를 명확히 구분하기 위한 방법으로 @Primary 어노테이션 사용이나 @Qualifier 어노테이션을 통한 명시적인 빈 선택에 대해 논의하고 있습니다. @Primary 어노테이션을 사용하면 스프링이 자동 주입을 할 때 해당 타입의 빈들 중 @Primary가 붙은 빈을 우선적으로 선택하게 됩니다. 반면, @Qualifier 어노테이션은 주입받을 대상이 되는 빈의 이름을 직접 지정하여, 여러 개의 후보 빈 중에서 특정 빈을 선택할 수 있도록 합니다.

또한, 본 강의의 다른 부분에서 동일한 타입의 다른 빈이 정의되어 있는지 확인해 보시고, 컴포넌트 스캔 대상에서 제외하거나, 특정 테스트에서만 사용할 빈을 별도로 정의하는 방법도 고려해 볼 수 있습니다.

저는 질문자님의 이해를 돕고자 지식공유자님의 기존 답변을 참고하여 유사한 답변 링크를 추천해드리고 있습니다.

현재 베타 기능으로 답변이 만족스럽지 않을 수 있는 점 양해 부탁드립니다. 🙏

추가적으로 궁금한 점이 있으시면, 이어서 질문해 주세요. 곧 지식공유자께서 답변해 주실 것입니다.

sin9801님의 프로필 이미지

작성한 질문수

질문하기