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>

如果还尝试了 autowire byName 或离开 @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 显然是配置但 spring 声称它不是?

我该如何解决这个问题?

编辑:

当我将 @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]
4

1 回答 1

0

解决方案:

http://blog.nigelsim.org/2011/05/31/spring-autowired-use-interfaces/

TestCompoundService 是一个具体的类。解决方案是创建一个接口并在代码中使用该接口,并在spring配置中使用具体类TestCompoundServiceImpl。

于 2013-02-11T13:49:12.060 回答