我有以下源示例,该示例在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)}
最佳答案
这是一个新的Spring Feature: Spring now treats generic types as a form of qualifier when injecting Beans-换句话说:autowire注意泛型!
您有BasicRunTest implements RunTest<Integer>
(整数),并向spring询问@Autowire prive RunTest<Number> runTest;
(数字)-不兼容!
尝试
private RunTest<? extends Number> runTest;
(它与Spring 3.x一起使用,或多或少是一个错误,因为您的代码违反了通用约束)