Annotation Based Configuration Hierarchy

We use the @Configuration classes to configure Spring-based Java. I am trying to create an AnnotationConfigApplicationContext (s) hierarchy.

It seems to have worked. Since I can Autowire beans from the parent context as beans members created from one of the child contexts.

However, I can’t manage Autowire beans from the parent context to the @Configuration class @Configuration , which is very convenient. All of them are equal to zero.

 // parent context config @Configuration public class ParentContextConfig{ @Bean parentBeanOne... @Bean parentBeanTwo... } 

 // child context config @Configuration public class ChildContextConfig{ @Autowired parentBeanOne @Bean childBeanOne... } 

 // a sample bean @Component public class ChildBeanOne{ @Autowired parentBeanTwo } 

In this example, I get parentBeanTwo correctly created, and parentBeanOne not auto-set ( null ) to the configuration file.

What am I missing?

+7
source share
3 answers

I think Spring wants you to use the standard Java hierarchy rules to establish the parent children of the configuration objects. That is, the child configuration class extends the parent configuration class.

0
source
0
source

To do this, your child context must import the parent context, for example:

 @Configuration @Import(ParentContextConfig.class) public class ChildContextConfig{ @Autowired parentBeanOne ... } 

For more information, see the spring doc on @Configuration .

0
source

All Articles