我有以下测试类:
@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]