与此类似question https://stackoverflow.com/questions/11777079/handling-several-implementations-of-one-spring-bean-interface,但我有这种情况。假设我有一个AccountService
接口和两个实现:DefaultAccountServiceImpl
and SpecializedAccountServiceImpl
,(这些类就像前面的question https://stackoverflow.com/questions/11777079/handling-several-implementations-of-one-spring-bean-interface)。实现在一个类中,但不同的方法有不同的 bean 实现。说:
@RestController
@RequestMapping("/account")
public class AccountManagerRestController {
@Autowired
private AccountService service;
@RequestMapping(value = "/register", method = RequestMethod.POST)
HttpEntity<?> registerAccount(@RequestBody AccountRegisterRequestBody input) {
// here the `service` is DefaultAccountServiceImpl
...
}
@RequestMapping(value = "/register/specialized", method = RequestMethod.POST)
HttpEntity<?> registerSpecializedAccount(@RequestBody AccountRegisterRequestBody input) {
// here the `service` is SpecializedAccountServiceImpl
...
}
}
Use @Qualifier("beanName")
@Autowired @Qualifier("service1")
private AccountService service1;
@Autowired @Qualifier("service2")
private AccountService service2;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)