ratediscount와 fixdiscount에 컴포넌트를 추가해준후로 계속오류가 발생하네요 이유가뭘까요?
455
작성한 질문수 86
[질문 내용]
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를 제거하면 해결됩니다.
.
감사합니다.
코드 자료
0
45
2
구현체가 동적으로 정해질 때, 팩토리 기법을 사용하나요?
0
55
2
MemberService의 인터페이스를 왜 사용하는지 궁금합니다.
0
76
1
롬복 @Setter를 써야 하는 상황이 있는건가요?
0
91
1
빈 등록 메서드의 파라미터가 빈이 아니어도 되나요?
0
81
1
테스트 속도가 나중에 영향이 있을까요?
0
77
1
gradle 설정 안떠서 질문 남깁니다!
0
121
2
build.gradle로 프로젝트를 여는 이유
0
86
1
provider 사용하는 이유
0
89
1
다음 강의 뭘 들어야 할까요
0
126
2
프로토타입 빈, 직접 destroy 호출 안 할 경우
0
66
1
beanB
0
82
2
퀴즈다시풀기
0
68
1
Gradle로 바꿔도 오류가 똑같이 발생하네요 ㅠㅠ
0
92
2
"중복 등록과 충돌" 강의에서 강사님과 다른 에러가 발생합니다.
0
67
3
run 실행했는데 결과창이 이렇게 뜨네요 왜 그런건가요>
0
106
2
도메인의 정의?
0
59
1
ApplicationContext 질문입니다.
0
63
1
@Scope의 proxyMode를 사용할때 단위 테스트 방법
0
89
2
ai api 선정하기 관련 질문
0
118
2
생성자 자동주입 관련해서
0
65
1
생성자 직접 호출 vs 팩토리 메서드 패턴
0
97
2
Spring에서 SessionScope와 RequestScope는 함께 사용되나요?
1
65
1
12:25
0
79
2





