簡體   English   中英

自動裝配在Spring 4中不起作用

[英]Autowiring doesn't work in Spring 4

我有以下源示例,該示例在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的一項新功能:在注入Bean時,Spring現在將泛型類型視為限定符的一種形式 -換句話說:自動裝配要注意泛型!

您有BasicRunTest implements RunTest<Integer> (Integer)並向spring請求@Autowire prive RunTest<Number> runTest; (數字)-不兼容!

嘗試

private RunTest<? extends Number> runTest;

(它與Spring 3.x一起使用,或多或少是一個錯誤,因為您的代碼違反了通用約束)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM