我正在尝试对不同的服务实现进行通用处理,但我经常收到“由于原始类型而导致未检查的调用”错误。
我尝试了几种实现,但不太明白这里的问题是什么。
我有以下模型:
public abstract class Fruit {
public List<String> vitamins;
public String originCountry;
// getters and setters omitted
}
具体实现如下:
public class Kiwi extends Fruit {
}
public class Pineapple extends Fruit {
}
我有以下界面:
public interface FruitCheckService<T extends Fruit> {
List<String> compareVitaminsFromDifferentCountries(T firstFruit, T secondFruit);
Class<T> getImplementation();
}
对于上面给出的 2 个模型的实现:
@Service
public class KiwiCheckServiceImpl implements FruitCheckService<Kiwi> {
@Override
public List<String> compareVitaminsFromDifferentCountries(Kiwi firstFruit, Kiwi secondFruit) {
// some implementation
return new ArrayList<>();
}
@Override
public Class<Kiwi> getImplementation() {
return Kiwi.class;
}
}
还有菠萝:
@Service
public class PineappleCheckServiceImpl implements FruitCheckService<Pineapple> {
@Override
public List<String> compareVitaminsFromDifferentCountries(Pineapple firstFruit, Pineapple secondFruit) {
// some implementation
return new ArrayList<>();
}
@Override
public Class<Pineapple> getImplementation() {
return Pineapple.class;
}
}
我有以下类正在使用不同的 bean 进行操作:
@Service
public class FruitServices {
private Map<Class, FruitCheckService> beansMap;
@Autowired
public FruitServices(List<FruitCheckService> fruitCheckServices) {
beansMap = new HashMap<>();
fruitCheckServices
.forEach(
fruitCheckService -> {
Class implementation = fruitCheckService.getImplementation();
beansMap.put(implementation, fruitCheckService);
}
);
}
public FruitCheckService getFruitCheckService(Class clazz) {
return beansMap.get(clazz);
}
}
POPMUISE
相关分类