Ser*_*gey 1 java generics spring inversion-of-control
我有以下源代码示例,它在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;
}
Run Code Online (Sandbox Code Playgroud)
如果我运行应用程序,我会得到异常:
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)}
Run Code Online (Sandbox Code Playgroud)
这是一个新的Spring特性:Spring现在将泛型类型视为注入Beans时的限定符形式 - 换句话说:autowire注意泛型类型!
你有BasicRunTest implements RunTest<Integer>(整数)并问春天@Autowire prive RunTest<Number> runTest;(数字) - 这是不兼容的!
尝试
private RunTest<? extends Number> runTest;
Run Code Online (Sandbox Code Playgroud)
(它与Spring 3.x一起使用,或多或少是一个bug,因为你的代码破坏了通用约束)
| 归档时间: |
|
| 查看次数: |
497 次 |
| 最近记录: |