简体   繁体   中英

Spring xml initializes beans when declared in child class but if the same is declared in parent class bean initialization fails

There are lot of Unitils based integration test classes in my application which uses @SpringApplicationContext (with different spring xmls from different modules) . In order to reduce the number of spring contexts being created, we planned to create a base test class and initialize the spring contexts only from this base class and make all the other tests to extend this base class.

base class:

com.org.module1.mypack;

@SpringApplicationContext({ "spring-module1.xml", "spring-module2.xml", "spring- module3.xml" }) public abstract class BaseTest{ ... }

child class before change:

com.org.module1.mypack.performance;

@SpringApplicationContext({ "spring-module4.xml" }) public class ChildTest extends BaseTest{ .. }

base class after change:

com.org.module1.mypack;

@SpringApplicationContext({ "spring-module1.xml", "spring-module2.xml", "spring- module3.xml", "spring-module4.xml" }) public abstract class BaseTest{ ... }

child class after change:

com.org.module1.mypack.performance;

//@SpringApplicationContext({ "spring-module4.xml" }) public class ChildTest extends BaseTest{ .. }

Now I get below error message and my context is not created: 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)}

When the spring-module4.xml was in the child class, the context was created normally and all the tests executed as usual.

Note: all the spring xml files are in src/main/resources, except spring-module4.xml is in src/test/resources

默认情况下,类的注释不继承,除非它们的定义中具有@Inherited。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM