2014-01-29 129 views
1

我有在Spring 3.2.6工作,但在4.0.1自动装配不春季工作4

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

你能告诉我们您的上下文配置?你有在任何地方定义的'RunTest' bean吗? –

+0

这会帮助你[弹簧和自动装配的泛型](http://www.jayway.com/2013/11/03/spring-and-autowiring-of-generic-types/) – EderRoger

回答

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的工作,或多或少是一个错误,因为你的代码打破通用约束)

+1

@ user3249497:堆栈溢出的常见方式说“谢谢”,就是接受答案(http://stackoverflow.com/about) – Ralph