2012-06-18 40 views
0

我使用Spring 3.0與genericDAO。我有這樣的:GenericDAO和NoSuchBeanDefinitionException:沒有唯一的bean Spring 3.0

public interface GenericDAO<T, ID extends Serializable> 
... 
@Repository("genericDAO") 
public class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T, ID> { 
... 
public interface A extends GenericDAO<Turno, Long> { 
... 
public interface B extends GenericDAO<TipoTurno, Long> { 
... 
@Repository("A") 
public class AImpl extends GenericDAOImpl<TipoTurno, Long> implements A{ 
... 
@Repository("B") 
public class BImpl extends GenericDAOImpl<TipoTurno, Long> implements B{ 

但是,當我試圖讓他們注入如下:

@Autowired 
A a; 

我得到:

預期單個匹配的bean,但發現3:genericDAOImpl,A ,B]

我不明白爲什麼。我也試圖與

@Resource(name="A") 

甚至

@Resource(type=A.class) 

我使用@Qualifier,但我總是得到相同的異常也試圖做到這一點,它看起來像春天一直在尋找GenericDao,而不是具體的類。

但它仍然無法正常工作。我不明白爲什麼。

有什麼建議嗎?

非常感謝。

+0

您確定在所有三種情況下都會得到相同的錯誤消息嗎?我可以看到如何在@Autowired情況下獲得「預期的單個匹配bean」消息,但絕對不在@Resource(name =「A」)情況下。是否有可能註釋了getter和成員變量? –

+0

非常感謝您的幫助。我已經加倍檢查它回答你,是的,當我使用@Resource(name =「specific_name」)我得到org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有類型[xxx.model.entities.dao。 GenericDAO]被定義爲:預期的單個匹配bean,但找到3:[genericDAO,A,B]。關於你的問題,我沒有註釋吸氣劑。 – CalamarBicefalo

+0

@CalamarBicefalo qualifer name與界面相同,這是錯誤的 – NimChimpsky

回答

1

我複製了你確切的錯誤信息,然後修復它。這裏是我使用的源代碼,減去軟件包名稱。我建議複製它,運行它,並與你的差異。我注意到的一個區別是,如問題中所寫,AImpl不能編譯。它實現GenericDAOImpl和GenericDAO。我將第一個通用參數改爲Turno,以便編譯它。我認爲這是一個錯字。當我最初將所有字段設置爲@Autowired時,我完全重現了您的錯誤。然後我添加了@Qualifier(「genericDAO」),併成功連線了這三個。

public interface GenericDAO<T, ID extends Serializable> {} 
... 
@Repository("genericDAO") 
public class GenericDAOImpl<T, ID extends Serializable> implements GenericDAO<T, ID> {} 
... 
public interface A extends GenericDAO<Turno, Long> {} 
... 
public interface B extends GenericDAO<TipoTurno, Long> {} 
... 
@Repository("A") 
public class AImpl extends GenericDAOImpl<Turno, Long> implements A {} 
... 
@Repository("B") 
public class BImpl extends GenericDAOImpl<TipoTurno, Long> implements B {} 
... 
public class TipoTurno {} 
... 
public class Turno {} 
... 
@Component 
public class Thingy { 
    @Autowired 
    private A a; 

    @Autowired 
    private B b; 

    @Autowired 
    @Qualifier(value="genericDAO") 
    private GenericDAO genericDao; 
} 
... 
public class Main { 
    public static void main(String[] args) { 
     ApplicationContext context = new ClassPathXmlApplicationContext("genericdao.xml"); 
     context.getBean(Thingy.class); 
    } 
} 
... 
<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
      http://www.springframework.org/schema/context 
      http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    <context:annotation-config/> 
    <context:component-scan base-package="genericdao"/> 
</beans> 

請注意,實際的錯誤消息比您提供的更長。閱讀尋找原因的整個事情很重要。 Spring喜歡5個甚至10個原因的異常堆棧痕跡。以下是我得到的消息的相關子字符串:

無法自動裝入字段:genericdao.GenericDAO genericdao.Thingy.genericDao;嵌套異常是org.springframework.beans.factory.NoSuchBeanDefinitionException:類型的無獨特豆[genericdao.GenericDAO]被定義:預期單一匹配豆但發現3:[A,B,genericDAO]

它表示未被自動裝配的字段是Thingy.genericDao,而不是Thingy.a。我強烈懷疑你的錯誤也是如此。

+0

它做到了。事實上,問題是我試圖注入一個'genericDAO'而沒有限定它。但它讓我發瘋,因爲我正在尋求解決它在特定的注射。非常感謝@John Watts,這非常棒。 「( – CalamarBicefalo

+0

)不客氣,謝謝你接受答案,我希望關於春季例外的評論是有幫助的,儘管他們經常包含必要的信息,但它可能會在海中迷路。 –

+0

仔細閱讀異常追蹤是我發現我的錯誤的方式,所以是的,您的評論真的很有幫助。 – CalamarBicefalo

0
@Repository("AImpl") 
public class AImpl extends GenericDAOImpl<TipoTurno, Long> implements A{ 
... 
@Repository("BImpl") 
public class BImpl extends GenericDAOImpl<TipoTurno, Long> implements B{ 
... 
@Resource(name = "AImpl") 
A whatever; //I would normally call it aImpl 

只要限定符名稱匹配,你應該很好去。該錯誤是由自動裝配注入引起的,其中接口實施了兩次。

+0

謝謝NimChimpsky,但是如上所述,當使用@Resource(name =「specific_name」)和@Resource(type = specific_class.class)時,我會得到完全相同的異常 – CalamarBicefalo

+0

@CalamarBicefalo看看我改變了什麼。您的界面名稱與導致問題的限定符相同。按照我概述的方式進行操作很好。 – NimChimpsky

+0

正如我上次的回覆所說,它仍然無法正常工作。但無論如何非常感謝你@NimChimpsky。我已經用實現名稱和隨機名稱來試用它。 – CalamarBicefalo

相關問題