Spring security spring 3测试:与@Autowired->;一起出现问题;can';不加载上下文

Spring security spring 3测试:与@Autowired->;一起出现问题;can';不加载上下文,spring-security,spring-test,Spring Security,Spring Test,我有以下测试课程: @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:ApplicationContext.xml", "classpath:SecurityContext.xml" }) public class CompoundServiceSecurityTest { @Autowired @Qualifier("testCompou

我有以下测试课程:

@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>
问题在于,
TestCompoundPermission
中的@Autowired testCompoundService由于依赖于testCompoundService本身而导致
NoSuchBeanDefinitionException
失败
TestCompoundService
依赖于StructureService,而StructureService依赖于StructureRepository(由Spring数据生成)。例外情况是:

NoSuchBeanDefinitionException: No matching bean of type myApp.repository.StructureRepository found
如果我在
TestCompoundPermission
中注释
@Autowired
,应用程序上下文将正确加载(当然,一些测试会失败,因为testCompoundService为null)

这很奇怪,因为很明显,在测试类上,加载完全正常的bean工作得非常好。为了完整性,我还尝试了一个构造函数参数,并删除了@Autowired。同样的结果。对我来说,这两个上下文看起来是完全分开的,因此安全上下文无法从应用程序上下文访问bean。我怎样才能解决这个问题

编辑:

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

编辑2:

我们发现:


这正是我的问题所在。但是,变通办法不起作用,因为
不再是有效的元素。

这不是加载配置文件的问题

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

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


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

事实证明,这是Spring数据的一般问题

请看


获取完整答案。

那么您是否有
myApp.repository.StructureRepository
bean?是(请参阅问题)。testCompoundService在测试类中已正确自动连接,但在该类中的测试中使用的某个类中未正确自动连接。是否可以发布stacktrace?
NoSuchBeanDefinitionException: No matching bean of type myApp.repository.StructureRepository found
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)