Skip to content

조회 빈이 2개이상 - 문제 #34

@mingsound21

Description

@mingsound21

@Autowired는 Type으로 조회를 한다.

FixDiscountPolicy와 RateDiscountPolicy 두 개 다 @component를 통해서 스프링 빈으로 등록하고, OrderServiceImpl에서 아래와 같이 자동 의존관계 주입을 설정하면, 에러가 발생한다.

DiscountPolicy 타입에 맞는 빈이 두개가 발견되기 때문이다. FixDisCountPolicy와 RateDiscountPolicy

@Autowired
  private DiscountPolicy discountPolicy

이때 private FixDiscountPolicy와 같이 하위 타입으로 고정할 수 있지만, 이는 DIP 위반이며, 유연성을 떨어트린다.

→ DIP 위반인 이유, 역할에 의존하는 것이 아니라, 구현에 의존하니까.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions