강의

멘토링

로드맵

Inflearn Community Q&A

hyunybutterchip's profile image
hyunybutterchip

asked

Spring Core Principles - Basic Edition

Option Processing

NoUniqueBeanDefinitionException 에러

Written on

·

1.5K

3

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'hello.core.discount.DiscountPolicy' available: expected single matching bean but found 2: rateDiscountPolicy,getDiscountPolicy

이 에러가 나서 원인을 찾아보니 AppConfig의

@Bean
public DiscountPolicy getDiscountPolicy() {
    //return new FixDiscountPolicy();
    return new RateDiscountPolicy();
}

이 부분과

@Component
public class RateDiscountPolicy implements DiscountPolicy {}

이 부분때문에 RateDiscountPolicy 빈이 2개로 등록되어서 그런 것 같아 AppConfig에서 @Bean어노테이션을 주석처리하니 정상적으로 테스트가 됐는데요.

 

궁금한 점은 AutoAppConfig 에서 AppConfig에 대한 부분은 제외를 시켰는데 왜 중복이 되는가입니다....

@Configuration
@ComponentScan(
        basePackages = "hello.core.member",
        excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes = Configuration.class)
) // 기존 AppConfig.java 때문에 컴포넌트 스캔 대상에서 제외시킴
public class AutoAppConfig {}

 

객체지향oopspring

Quiz

What are the main reasons why Field Injection is generally not recommended?

Because it is difficult to guarantee immutability

Because changing dependencies is easy

Because it's difficult to test with pure Java code

Because writing configuration code is complex

Answer 3

0

yh님의 프로필 이미지
yh
Instructor

안녕하세요. hyun님

전체 프로젝트를 압축해서 구글 드라이브로 공유해서 링크를 남겨주세요.

구글 드라이브 업로드 방법은 다음을 참고해주세요.

https://bit.ly/3fX6ygx

주의: 업로드시 링크에 있는 권한 문제 꼭 확인해주세요

추가로 다음 내용도 코멘트 부탁드립니다.

1. 실행 방법을 알려주세요.

2. 어떻게 문제를 확인할 수 있는지 자세한 설명을 남겨주세요.

감사합니다.

 

혹시 해결 되셨나요? 저도 같은 오류여서요

0

hyun님의 프로필 이미지
hyun
Questioner

basePackages 부분을 빼니

 

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'hello.core.discount.DiscountPolicy' available: expected single matching bean but found 2: rateDiscountPolicy,getDiscountPolicy

 

이 오류가 나네요 ....

혹시 AppConfig 클래스와 AutoAppConfig 클래스가 다른 패키지에 있지는 않죠?

hyun님의 프로필 이미지
hyun
Questioner

네 같은 패키지에 있어용,,

0

혹시 basePackageshello.core.member 패키지로 설정되어 있어서 그런 건 아닐까요??!

+ 생각해 보니 아예 base package가 member 패키지이면 회원 관련 클래스를 제외하면 다 스캔 대상에서 제외될텐데... 이상하네여..ㅠㅜ

hyun님의 프로필 이미지
hyun
Questioner

엇.. 그러네요 .. basePackages 때문에 RateDiscountPolicy 이것도 빈 등록이 안된거 아닌가 싶은데... ㅜㅜ 어렵네요

hyunybutterchip's profile image
hyunybutterchip

asked

Ask a question