2013-02-11 95 views
0

我有以下測試類:彈簧自動裝配數據不工作

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("classpath:ApplicationContext.xml") 
public class CompoundServiceImplTest { 

    @Autowired 
    @Qualifier("testCompoundService") 
    private TestCompoundService testCompoundService; 

    //... 
} 

和ApplicationContext中包含:

<bean id="testCompoundService" autowire="byType" 
     class="myPackage.TestCompoundService"> 
</bean> 

如果還試圖自動裝配綽號或離開@Qualifier遠(我補充說,因爲它沒有不工作,但也沒有幫助)。

我得到以下異常:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
    No matching bean of type [myPackage.TestCompoundService] 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), 
     @org.springframework.beans.factory.annotation.Qualifier(value=testCompoundService)} 

這個bean顯然還配置春季聲稱它是不是?

我該如何解決這個問題?

編輯:

當我改變@Autowired到@Resource我獲得以下錯誤:

Injection of resource dependencies failed; 
    nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: 
    Bean named 'testCompoundService' must be of type [myPackage.TestCompoundService], 
    but was actually of type [$Proxy68] 
+0

相關/重複:http://stackoverflow.com/questions/14509142,http://stackoverflow.com/questions/11535527,http://stackoverflow.com/questions/8391944和其他許多人 – 2013-02-11 18:30:35

回答