복붙노트

[SPRING] 하나의 Spring 빈 / 인터페이스에 대한 여러 구현의 처리

SPRING

하나의 Spring 빈 / 인터페이스에 대한 여러 구현의 처리

스프링 빈의 여러 구현에 의존해야한다고 가정 해 보겠습니다. 하나의 AccountService 인터페이스와 두 개의 구현 : DefaultAccountServiceImpl 및 SpecializedAccountServiceImpl이 있습니다.

해결법

  1. ==============================

    1.광고. 1 : @Autowired 대신 @Resource를 사용하여 @Qualifier 주석 또는 autowire를 사용할 수 있습니다. @Autowired는 입력하지 않고 필드 이름을 기본값으로 사용합니다.

    광고. 1 : @Autowired 대신 @Resource를 사용하여 @Qualifier 주석 또는 autowire를 사용할 수 있습니다. @Autowired는 입력하지 않고 필드 이름을 기본값으로 사용합니다.

    광고. 2 : 실행시에 두 개의 빈이이 인터페이스를 구현한다고 말하면 실패합니다. 콩 중 하나에 @Primary가 추가로 주석으로 추가되면 유형별로 자동 와이어 링 할 때 선호됩니다.

  2. ==============================

    2.

    @Autowired
    @Qualifier("impl1")
    BaseInterface impl1;
    
    @Autowired
    @Qualifier("impl2")
    BaseInterface impl2;
    
    @Component(value="impl1")
    public class Implementation1  implements BaseInterface {
    
    }
    
    @Component(value = "impl2")
    public class Implementation2 implements BaseInterface {
    
    }
    
    
    For full code: https://github.com/rsingla/springautowire/
    
  3. from https://stackoverflow.com/questions/11777079/handling-several-implementations-of-one-spring-bean-interface by cc-by-sa and MIT license