创建具有多个子上下文的 Spring Boot 应用程序 [英] Create a spring boot application with multiple child contexts

查看:80
本文介绍了创建具有多个子上下文的 Spring Boot 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用具有分层应用程序上下文的 Spring Boot 创建应用程序.我目前的主要方法如下:

I'm trying to create an application using spring boot with an hierarchical application context. My current main method looks like:

public static void main(String[] args) {
    new SpringApplicationBuilder(TestApplication.class)
            .child(AuditServiceConfiguration.class).web(true)
            .child(TaskServiceConfiguration.class).web(true)
            .run(args);
}

并且两个子配置被注释为:

and the two children configurations are annotated with:

@EnableAutoConfiguration
@Configuration

这个想法是有一个包含所有公共 bean 的父上下文和每个子上下文来运行自己的 MVC,同时与它的兄弟姐妹隔离.

The idea is to have a parent context containing all common beans and each child context to run its own MVC while being isolated from its siblings.

不幸的是,当我运行上面的代码时,只初始化并启动了最后一个子上下文.

Unfortunately when I run the above, only the last child context is initialised and started.

任何指向正确方向的指针将不胜感激.

Any pointers in the right direction would be greatly appreciated.

问候,

亚历山德罗

推荐答案

child(...) 方法创建并返回另一个SpringApplicationBuilder>,所以当你调用第二个 child(...) 方法时,你不是在为孩子实例化一个兄弟,而是在第一个孩子上创建一个孩子,这使得父母成为 爷爷.

The child(...) method creates and returns another SpringApplicationBuilder, so when you call that second child(...) method, you are not instantiating a brother for the child, you are making a child on the first child, which makes the parent become a grandpa.

抛开笑话,看看sibling(...) 方法,该方法允许创建具有相同父级的另一个上下文.

Jokes aside, have a look at the sibling(...) method which allows to create another context with the same parent.

你也可以查看source 看看到底发生了什么.

You can also check out the source to see exactly what is going on.

这篇关于创建具有多个子上下文的 Spring Boot 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆