강의

멘토링

로드맵

Inflearn Community Q&A

chldd125557's profile image
chldd125557

asked

Spring Core Principles - Basic Edition

@Autowired Field Name, @Qualifier, @Primary

조회빈이 2개 이상있을때 @Autowired를 사용시에 생성자에서 발생하는 오류

Resolved

Written on

·

648

0

[질문 템플릿]
1. 강의 내용과 관련된 질문인가요? (예/아니오)
2. 인프런의 질문 게시판과 자주 하는 질문에 없는 내용인가요? (예/아니오)
3. 질문 잘하기 메뉴얼을 읽어보셨나요? (예/아니오)

[질문 내용]
조회빈이 2개 이상일때 @Autowired 어노테이션이 붙은 생성자가 의존성을 주입할시에 처음에는 타입으로 주입시도하고 아닐경우에 필드명으로 주입을 시도한다고 하는데요. 필드명을 주입하려는 구현체랑 맞추더라도 같은 에러가 발생하는데요 혹시 해당 이슈에 대해서 업데이트 된 부분이 있을까요 ? 혹시나 필드명을 제가 잘못입력했을까봐 에러 로그에서 복사해서 실행해도 같은 증상이 나타압니다.

 

다음은 에러 코드입니다.

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'orderServiceImpl' defined in file [/Users/choehyeonseong/Desktop/project/spring/core/out/production/classes/hello/core/order/service/OrderServiceImpl.class]: Unsatisfied dependency expressed through constructor parameter 1: No qualifying bean of type 'hello.core.discount.DiscountPolicy' available: expected single matching bean but found 2: fixDiscountPolicy,rateDiscountPolicy

 

다음은 생성자 부분입니다.

    @Autowired
    public OrderServiceImpl(MemberRepositroy memberRepositroy, DiscountPolicy rateDiscountPolicy/*autowired등록시에 처음에 타입으로 조회 다음에 필드 변수명으로 매칭한다.*/) {
        System.out.println("1. OrderServiceImpl.OrderServiceImpl");
        this.memberRepositroy = memberRepositroy;
        this.discountPolicy = rateDiscountPolicy;
    }
spring객체지향

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 1

2

Super Novice님의 프로필 이미지
Super Novice
Questioner

조금더 찾아보니 이미 부트 3.2버전에서 해당 에러가 발생하고 자주 하는 질문 리스트?에 해결방안이 있었습니다. https://docs.google.com/document/d/1j0jcJ9EoXMGzwAA2H0b9TOvRtpwlxI5Dtn3sRtuXQas/edit#heading=h.b1yk4ued1pxo

링크를 통해서 해결하였습니다.

chldd125557's profile image
chldd125557

asked

Ask a question