弹簧数据自动装配不起作用

时间:2013-02-11 12:05:57

标签: spring autowired

我有以下测试类:

@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)}

豆子显然是配置但春天声称它不是吗?

我该如何解决这个问题?

编辑:

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

1 个答案:

答案 0 :(得分:0)

解决方案:

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

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