在我的应用程序中有很多基于Unitils的集成测试类,它使用@SpringApplicationContext
(与来自不同模块的不同spring xmls)。为了减少正在创建的spring上下文的数量,我们计划创建一个基本测试类并仅从此基类初始化spring上下文,并使所有其他测试扩展此基类。Spring xml在子类中声明时初始化bean,但是如果在父类中初始化bean初始化失败
基类:
com.org.module1.mypack;
变更前@SpringApplicationContext({ “弹簧module1.xml”, “弹簧module2.xml”, “弹簧module3.xml”}) 公共抽象类BaseTest { ... }
子类:
com.org.module1.mypack.performance;
@SpringApplicationContext({ “弹簧module4.xml”}) 公共类ChildTest延伸BaseTest { .. }变更后
基类:
com.org.module1.mypack;
@SpringApplicationContext({ “弹簧module1.xml”, “弹簧module2.xml”, “弹簧module3.xml”, “弹簧module4.xml”}) 公共抽象类BaseTest { ... }变更后
子类:
com.org.module1.mypack.performance;
// @ SpringApplicationContext({ “弹簧module4.xml”}) 公共类ChildTest扩展BaseTest { .. }
现在,我得到以下错误消息,我的上下文没有被创建: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.org.module5.prepare.MyBean] 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=myBean)}
当spring-module4.xml在子类中时,上下文被正常创建并且所有测试都照常执行。
注意:除spring-module4外,所有spring xml文件都位于src/main/resources中。xml在src/test/resources中
'NoSuchBeanDefinitionException'清楚地表明父类没有MyBean的可见性。这对任何人来说都响个不停? – EmeraldTablet