Java – annotation based configuration hierarchy

We use the @ configuration class for Java - based spring configuration I'm trying to set the hierarchy of annotationconfigapplicationcontext

It seems to work Because I can automatically use beans from the parent context as members of beans created from one of the child contexts

But I didn't manage the auto - assembled bean from the parent context to the @ configuration class file, which is very convenient They are all empty

// 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, what I get is that parentbeantwo is created correctly, while parentbeanone is not automatically assembled (null) to the configuration file

What did I miss?

Solution

I think spring wants you to use standard Java hierarchy rules to parent and child configuration objects That is, let the child configuration class extend the parent configuration class

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>