bal*_*teo 17 java spring interface
说我需要依赖Spring bean的几个实现.我有一个AccountService接口和两个实现:DefaultAccountServiceImpl和SpecializedAccountServiceImpl.
这怎么可能(注入一个或另一个实现)在Spring?
以下注射使用哪种实施方案?
@Autowired
private AccountService accountService;
Run Code Online (Sandbox Code Playgroud)Tom*_*icz 20
广告.1:您可以使用@Qualifier注释或自动装配@Resource,而不是@Autowired默认使用字段名称而不是键入.
广告.2:它将在运行时失败,说两个bean正在实现此接口.如果其中一个bean另外注释了@Primary,则在按类型自动装配时将首选.
小智 14
@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/
Run Code Online (Sandbox Code Playgroud)