如果有两个类型信息不同的实例,则键入以将泛型接口的类型化组件注入泛型类失败。
org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'hello.ObjectPrinter<?>' available: expected single matching bean but found 2: integerPrinter,stringPrinter
我想知道这是否是预期的结果,是否有足够的类型信息?
我得到的结果是
ObjectPrinter
但如果@Component
public class ObjectPrinter<T> {
public String print(T obj) {
return obj.toString();
}
}
是通用类,那么它确实有效,因此类型信息必须仍然可用
@Configuration
如果我使用@Bean
和BetterObjectPrinter
注释创建2个bean来创建itemid description category
11802974 SPRO VUH3C1 DIFFUSER VUH1 TRIPLE Space heaters Architectural Diffusers
10688548 ANTIQUE BRONZE FINISH PUSHBUTTON switch Door Bell Pushbuttons
9836436 Descente pour Cable tray fitting and accessories Tray Cable Drop Outs
使用Spring 4.3.13测试以上内容。
答案 0 :(得分:0)
是的,它预计,春天不知道哪两个自动连接到objectPrinter字段。您可以使用@Qualifier注释来解决冲突。