![](/img/trans.png)
[英]How to refer a bean declared in spring context xml from an annotated class spring
[英]Spring xml initializes beans when declared in child class but if the same is declared in parent class bean initialization fails
我的应用程序中有很多基于Unitils的集成测试类,它们使用@SpringApplicationContext
(来自不同模块的spring @SpringApplicationContext
有所不同)。 为了减少创建的Spring上下文的数量,我们计划创建一个基测试类,并仅从该基类初始化spring上下文,并进行所有其他测试以扩展该基类。
基类:
com.org.module1.mypack;
@SpringApplicationContext({“ spring-module1.xml”,“ spring-module2.xml”,“ spring-module3.xml”})公共抽象类BaseTest {...}
变更前的子班:
com.org.module1.mypack.performance;
@SpringApplicationContext({“ spring-module4.xml”})公共类ChildTest扩展了BaseTest {..}
更改后的基类:
com.org.module1.mypack;
@SpringApplicationContext({“ spring-module1.xml”,“ spring-module2.xml”,“ spring-module3.xml”,“ spring-module4.xml”})公共抽象类BaseTest {...}
更改后的子班:
com.org.module1.mypack.performance;
// @ SpringApplicationContext({“ spring-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)}
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 xml文件都在src / main / resources中,但spring-module4.xml不在src / test / resources中
默认情况下,类的注释不继承,除非它们的定义中具有@Inherited。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.