【发布时间】:2016-04-20 15:46:49
【问题描述】:
如何将存储库注入泛型类?
public class FruitComboBox<T extends Fruit> extends ComboBox {
@Autowired
private JpaRepository<T, Integer> repository;
...
}
public class FruitMarket {
@Autowired
FruitComboBox<Apple> appleCombobox; // Apple extends Fruit
@Autowired
FruitComboBox<Orange> orangeCombobox; // Orange extends Fruit
...
}
我还有两个仓库
@Repository
public interface AppleRepository extends JpaRepository<Apple, Integer> {
}
@Repository
public interface OrangeRepository extends JpaRepository<Orange, Integer> {
}
我认为来自 FruitComboBox 的通用存储库应该根据它的 T 解析为两个现有存储库之一,并由 Spring 自动装配。
NoUniqueBeanDefinitionException: expected single matching bean but found 2 发生在运行时(不是在应用程序启动时)。所以我认为在运行时定义了所有类型,并且 Spring 已经知道具体类型是 T。
【问题讨论】:
-
这方面有什么进展吗?我有一个类似的问题,对我来说,这听起来像一个错误!
标签: java generics spring-boot autowired