次のテストクラスがあります。
@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;
//...
}
問題は、@AutowiredtestCompoundServiceがtestCompoundService自体の依存関係でTestCompoundPermission
失敗することです。StructureRepository(Spring Dataによって生成される)に依存するStructureServiceに依存します。例外は次のとおりです。NoSuchBeanDefinitionException
TestCompoundService
NoSuchBeanDefinitionException: No matching bean of type myApp.repository.StructureRepository found
アプリケーションコンテキストでコメント@Autowired
するとTestCompoundPermission
、正しく読み込まれます(もちろん、testCompoundServiceがnullであるため、一部のテストはNullPointerExceptionで失敗します)。
明らかにテストクラスでは、正確な正気の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 />
ただし、有効な要素ではなくなったため、回避策は機能しません。