작성
·
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
프로젝트 코드 확인해보았습니다.
질문 내용을 확인하기 위해선
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를 제거하면 해결됩니다.
.
감사합니다.
감사합니다