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

666 Views Asked by At

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)}

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

1

There are 1 best solutions below

2
Eugene On

By default annotations on classes are not inherited, unless they have @Inherited in their definition.