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

성문님의 프로필 이미지
성문

작성한 질문수

스프링 핵심 원리 - 기본편

@Autowired 필드 명, @Qualifier, @Primary

ratediscount와 fixdiscount에 컴포넌트를 추가해준후로 계속오류가 발생하네요 이유가뭘까요?

작성

·

382

1


[질문 내용]
Error creating bean with name 'orderServiceImpl' defined in file [C:\hello-spring\hello-core\bin\main\hello\core\order\OrderServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'hello.core.discount.DiscountPolicy' available: expected single matching bean but found 2: rateDiscountPolicy,discountPolicy

구글드라이브 주소입니다 : https://drive.google.com/drive/folders/1uhboQSI7MAc3eD3NIM_9B-H4p37IYT14?usp=share_link

 

 

답변 1

3

안녕하세요. 성문님, 공식 서포터즈 OMG입니다.
.

프로젝트 코드 확인해보았습니다.

질문 내용을 확인하기 위해선

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

 

생성자를 제거하거나 , @RequiredArgsConstructor 어노테이션을 제거해야하구요,

 

@Component
//@RequiredArgsConstructor ==> "제거해야함-OMG"
public class MemberServiceImpl implements MemberService {
   
   private final MemberRepository memberRepository;
   
   @Autowired
   public MemberServiceImpl(MemberRepository memberRepository) {
      this.memberRepository = memberRepository;
   }
   public void join(Member member) {
      memberRepository.save(member);
   }
   public Member findMember(Long memberId) {
      return memberRepository.findById(memberId);
   }
   //테스트 용도
    public MemberRepository getMemberRepository() {
       return memberRepository;
    }

} 

 

//@RequiredArgsConstructor ==> "제거해야함-OMG"
@Component
public class OrderServiceImpl implements OrderService {
   private  final MemberRepository memberRepository;
   private final DiscountPolicy discountPolicy;
   @Autowired
   public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy ratediscountPolicy) {
   this.memberRepository = memberRepository;
    this.discountPolicy = ratediscountPolicy;
 }
 @Override
   public Order createOrder(Long memberId, String itemName, int itemPrice) {
    Member member = memberRepository.findById(memberId);
    int discountPrice = discountPolicy.discount(member, itemPrice);
      return new Order(memberId, itemName, itemPrice, discountPrice);
 }

 
 
 
//테스트 용도
   public MemberRepository getMemberRepository() {
      return memberRepository;
}
 
 
}

 

제거하고 나면 질문의 에러가 출력되는데요.

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

 

이 에러는 에러메시지에도 나와있듯이 동일타입 빈이 두개가 등록되어 발생합니다.

RateDiscountPolicy와

@Component
public class RateDiscountPolicy implements DiscountPolicy {
 private int discountPercent = 10; //10% 할인
 @Override
 public int discount(Member member, int price) {
    if (member.getGrade() == Grade.VIP) {
       return price * discountPercent / 100;//
    } else {
       return 0;
    }
   }
}

AutoAppConfig의 @Bean DiscountPolicy로 빈(bean) 중복이 발생합니다.

 

@Configuration

@ComponentScan( 
      basePackages = "hello.core",
      basePackageClasses = AutoAppConfig.class,
      excludeFilters = @ComponentScan.Filter(type = FilterType.ANNOTATION, classes =Configuration.class))

public class AutoAppConfig {
   
       @Bean(name = "discountPolicy")
       public DiscountPolicy discountPolicy(){

           return new RateDiscountPolicy();
       }
}

 @Component 혹은

@Bean(name = "discountPolicy")

을 제거하거나, @Component를 제거하면 해결됩니다.

.
감사합니다.

성문님의 프로필 이미지
성문
질문자

감사합니다

성문님의 프로필 이미지
성문

작성한 질문수

질문하기