我有以下源代码示例,它在Spring 3.2.6中有效但在4.0.1中不起作用
public interface RunTest<T extends Number> {
void run(T number);
}
public class BasicRunTest implements RunTest<Integer>{
@Override
public void run(Integer number) {
}
}
@Component
public class BeanTest {
@Autowired
private RunTest<Number> runTest;
}
如果我运行应用程序,我会得到例外:
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.test.RunTest] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
答案 0 :(得分:5)
这是一个新的Spring Feature: Spring now treats generic types as a form of qualifier when injecting Beans - 换句话说:autowire注意泛型类型!
你有BasicRunTest implements RunTest<Integer>
(整数)并问春天@Autowire prive RunTest<Number> runTest;
(数字) - 这是不兼容的!
尝试
private RunTest<? extends Number> runTest;
(它适用于Spring 3.x,或多或少是一个bug,因为你的代码破坏了通用约束)