spring 3测试:@Autowired问题 - >无法加载Context

时间:2013-02-25 08:32:53

标签: spring-security spring-test

我有以下测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:ApplicationContext.xml",
    "classpath:SecurityContext.xml"
})
public class CompoundServiceSecurityTest {

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

    //...
}

在此类中,testCompoundService正确自动装配并正常工作(如果实际问题是“禁用”)。安全上下文包含以下自定义类:

<bean id="testCompoundPermission"
      class="myPackage.TestCompoundPermission">
</bean>

该类使用自动连接的testCompoundService:

public class TestCompoundPermission extends AbstractPermission {

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

    //...
}

问题是TestCompoundPermission中的@Autowired testCompoundService在testCompoundService本身的依赖关系上失败并带有NoSuchBeanDefinitionExceptionTestCompoundService依赖于StructureService,它依赖于StructureRepository(由Spring Data生成)。例外是:

NoSuchBeanDefinitionException: No matching bean of type myApp.repository.StructureRepository found

如果我在@Autowired中评论TestCompoundPermission,那么应用程序上下文正确加载(当然,一些测试因NullPointerException而失败,因为testCompoundService为null)。

这很奇怪,因为很明显在测试类中,精确理智的bean的加载效果非常好。为了完整性,我还尝试使用构造函数参数并删除@Autowired。相同的结果。对我来说,看起来2个上下文是完全分开的,因此安全上下文无法从应用程序上下文中访问bean。我该如何解决这个问题?

编辑:

stacktrace的最后一部分(整个堆栈跟踪很大!)

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testCompoundService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myApp.service.StructureService myApp.service.CompoundServiceImpl.StructureService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'StructureService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: myApp.repository.StructureRepository myApp.service.StructureServiceImpl.structureRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [myApp.repository.StructureRepository] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:871)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:813)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private myApp.service.StructureService myApp.service.CompoundServiceImpl.StructureService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'StructureService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: myApp.repository.StructureRepository myApp.service.StructureServiceImpl.structureRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [myApp.repository.StructureRepository] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'StructureService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: myApp.repository.StructureRepository myApp.service.StructureServiceImpl.structureRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [myApp.repository.StructureRepository] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:871)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:813)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: myApp.repository.StructureRepository myApp.service.StructureServiceImpl.structureRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [myApp.repository.StructureRepository] 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)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:513)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:92)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [myApp.repository.StructureRepository] 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)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:947)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:816)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:485)

编辑2:

发现这个:

http://forum.springsource.org/showthread.php?113720-No-matching-bean-when-using-custom-PermissionEvaluator-and-Spring-Data-JPA

这就是我遇到的问题。但是,由于<jpa:repository />不再是有效元素,因此解决方法不起作用。

2 个答案:

答案 0 :(得分:0)

加载配置文件不是问题

您在xml文件中定义了 bean及其属性的问题。

检查 .xml文件是否已在测试类中使用了正确的bean引用及其属性引用。

<强> EG)。

必须在您调用的任何 .XML中定义

testCompoundService Bean

答案 1 :(得分:0)

事实证明这是Spring数据的一个普遍问题。

请参阅

https://jira.springsource.org/browse/SEC-2136

获得完整答案。