[英]What is the best approach to get injected beans with same interface in factory using Spring?
我创建了一个工厂,根据一些条件检查来决定应返回哪种最佳实施。
// Factory
@Component
public class StoreServiceFactory {
@Autowired
private List<StoreService> storeServices;
public StoreService getService(){
if(isActiveSale){
return storeServices.get("PublicStoreService")
}
return storeServices.get("PrivateStoreService")
}
}
//Service Implementations
@Service
@Qualifier("PublicStoreService")
public class PublicStoreService implements StoreService {
public getStoreBalanceScore(){
Do Stuff....
}
}
@Service
@Qualifier("PrivateStoreService")
public class PrivateStoreService implements StoreService {
public getStoreBalanceScore(){
Do Stuff....
}
}
// Controller
@Autowired
StoreServiceFactory storeServiceFactory;
@Override
public StoreData getStoreBalance(String storeId) {
StoreService storeService = storeServiceFactory.getService();
return simulationService.simulate(sellerId, simulation);
}
这种方法好吗? 如果是,我如何以一种优雅的方式获得服务? 我只想使用注释,而不使用配置。
您应该使用映射而不是List,并将字符串参数传递给getService方法。
public class StoreServiceFactory {
@Autowired
private Map<String,StoreService> storeServices = new HashMap<>();
public StoreService getService(String serviceName){
if(some condition...){
// want to return specific implementation on storeServices map, but using @Qualifier os something else
storeServices.get(serviceName)
}
}
}
您可以使用支持的实现来预填充地图。 然后,您可以获取适当的服务实例,如下所示:
// Controller
@Autowired
StoreServiceFactory storeServiceFactory;
@Override
public StoreData getStoreBalance(String storeId) {
StoreService storeService = storeServiceFactory.getService("private");//not sure but you could pass storeId as a parameter to getService
return simulationService.simulate(sellerId, simulation);
}
如果您不喜欢使用字符串,则可以为受支持的实现定义一个枚举,并将其用作映射的键。
您无需在代码上创建列表或映射。 您可以使用GenericBeanFactoryAccessor从Spring上下文直接检索它。 这有多种方法来检索特定的bean,例如基于名称,注释等。您可以在此处查看javadoc。 这避免了不必要的复杂性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.