【问题标题】:spring-data autowiring not working弹簧数据自动装配不起作用
【发布时间】:2023-03-26 00:10:01
【问题描述】:

我有以下测试类:

@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 已明确配置,但 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]

【问题讨论】:

标签: spring autowired


【解决方案1】:

解决办法:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-29
    • 2013-08-23
    • 1970-01-01
    • 1970-01-01
    • 2012-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多