我有以下测试类:
@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;
//...
}
问题是 @Autowired testCompoundService因依赖 testCompoundService 本身而TestCompoundPermission
失败。依赖于 StructureService,后者依赖于 StructureRepository(由 Spring Data 生成)。例外是:NoSuchBeanDefinitionException
TestCompoundService
NoSuchBeanDefinitionException: No matching bean of type myApp.repository.StructureRepository found
如果我@Autowired
在TestCompoundPermission
应用程序上下文中正确加载评论(当然,一些测试会因 NullPointerException 而失败,因为 testCompoundService 为空)。
这很奇怪,因为显然在测试类上加载完全正常的 bean 工作得非常好。为了完整起见,我还尝试使用构造函数参数并删除了@Autowired。结果相同。对我来说,看起来 2 个上下文是完全分离的,因此安全上下文无法从应用程序上下文访问 bean。我该如何解决这个问题?
编辑:
堆栈跟踪的最后一部分(整个堆栈跟踪很大!)
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:
发现这个:
这正是我遇到的问题。但是,解决方法不起作用,因为<jpa:repository />
它不再是有效元素。