해결된 질문
작성
·
512
0
[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? (예/아니오) 예
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예/아니오) 예
3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예/아니오)
예
[질문 내용]
안녕하세요. 컴포넌트 스캔쪽 강의를 따라가는데 에러메시지가 나와서 원인을 찾아보려해도 도저히 보이지가 않아 질문드립니다. ㅠㅠ
에러 메시지.
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderServiceImpl' defined in file [/Users/jhj/Desktop/study/spring_standard_kyh/core/out/production/classes/hello/core/order/OrderServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'hello.core.member.MemberRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
구글링 해본 결과, OrderServiceImpl의 생성자 주입 과정에서 첫번째 파라미터에 문제가 있는것 같습니다만, 아무리 찾아봐도 원인을 모르겠습니다....
@Component
public class OrderServiceImpl implements OrderService{
// private final MemberRepository memberRepository = new MemoryMemberRepository();
// private final DiscountPolicy discountPolicy = new FixDiscountPolicy();
// private final DiscountPolicy discountPolicy = new RateDiscountPolicy();
// private DiscountPolicy discountPolicy; // 인테페이스에만 의존 하지만 널포인트 예외가 발생.
// 해결하려면 누군가 객체를 생성해서 주입해주면 될것 같다?
private final MemberRepository memberRepository;
private final DiscountPolicy discountPolicy;
@Autowired
public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) {
this.memberRepository = memberRepository;
this.discountPolicy = discountPolicy;
}
@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;
}
}
//-----------------------------
@Component
public class MemberServiceImpl implements MemberService{
private final MemberRepository memberRepository;
//생성자에 @Autowired를 사용하면, 스프링이 자동으로 생성자 주입 해줌.
// ac.getBean(MemberRepository.class)를 한것과 같은 효과.
@Autowired
public MemberServiceImpl(MemberRepository memberRepository) {
this.memberRepository = memberRepository;
}
@Override
public void join(Member member) {
memberRepository.save(member);
}
@Override
public Member findMember(Long memberId) {
return memberRepository.findById(memberId);
}
//테스트 용도
public MemberRepository getMemberRepository() {
return memberRepository;
}
}