java 我们什么时候使用 SpringApplicationBuilder?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45911357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 08:58:56  来源:igfitidea点击:

When do we use a SpringApplicationBuilder?

javaspringspring-boot

提问by Ole

I'm going through a Spring Microservices tutorial and it has the following line in it:

我正在阅读 Spring 微服务教程,其中包含以下行:

new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args);

Most of the time I see this to startup a Spring Boot application:

大多数时候我看到这个是为了启动一个 Spring Boot 应用程序:

SpringApplication.run(Application.class, args);

This is the first time I have seen SpringApplicationBuilder. When should we use this in general?

这是我第一次看到 SpringApplicationBuilder。我们一般什么时候应该使用它?

采纳答案by Saravana

In our application we have used SpringApplicationBuilderin the starterapplication. starteris an simple application which will start the actual application instances programatically.

在我们的应用程序中我们已经SpringApplicationBuilderstarter应用程序中使用过。starter是一个简单的应用程序,它将以编程方式启动实际的应用程序实例。

The number of processes to be started and type of process web/standalone will be passed as an argument to starterapplication, based on the arguments the application instances will be started. we have used -wto start as web application for state management.

要启动的进程数和进程 web/standalone 的类型将作为参数传递给starter应用程序,基于将启动应用程序实例的参数。我们过去常常-w作为状态管理的 Web 应用程序开始。

boolean isWeb = // options parser, parse -w
new SpringApplicationBuilder(SpringBootAngularApp.class).web(isWeb).run(args);

There is another way of doing the same

还有另一种方法可以做同样的事情

SpringApplication sp = new SpringApplication(SpringApplicationBuilder.class);       
sp.setWebEnvironment(false);
sp.run(args);

We can also customisebanner, loggers with SpringApplicationBuilder.

我们还可以使用SpringApplicationBuilder.

read the doc for more usage

阅读文档以了解更多用法

回答by Daniel C.

Let's say that you have to solve a problem where you need to work with multiple databases or structures and each one needs to be isolated from the other, in that case I would use a SpringApplicationBuilderapproach, because every domain can be isolated through the creation of parent and child context and there is no need to mix different domain problems, for example you could have Application1 and Application2 configuration, each one with its own domains, controllers, and repositories but you don't want to mix all this complixity and instead of that you can create two different configuration with SpringApplicationBuilder

假设您必须解决一个问题,您需要使用多个数据库或结构,并且每个数据库或结构都需要相互隔离,在这种情况下,我将使用一种SpringApplicationBuilder方法,因为每个域都可以通过创建父域来隔离和子上下文,并且不需要混合不同的域问题,例如,您可以有 Application1 和 Application2 配置,每个配置都有自己的域、控制器和存储库,但您不想混合所有这些复杂性,而不是那些您可以创建两种不同的配置SpringApplicationBuilder

 SpringApplicationBuilder appBuilder = new SpringApplicationBuilder()
        .sources(Parent.class);

 appBuilder.child(Application1.class).run(args);
 appBuilder.child(Application2.class).run(args);

Some additional information: Post with an example of SpringApplicationBuilder, Java Doc of SpringBuilderand Other example of how use SpringApplicationBuilder

一些额外的信息:邮政与SpringApplicationBuilder的例子SpringBuilder的Java文档以及如何使用SpringApplicationBuilder的其他例子

回答by Sabir Khan

One common use case that I came across is when you wish to have a traditional deployment war file to be deployed in Weblogic etc - Traditional deployment

我遇到的一个常见用例是,当您希望在 Weblogic 等中部署传统部署 war 文件时 -传统部署

With SpringApplication, most of application settings have hard coded default values like profiles and property files to use etc. You need to look at this class's code to understand that.

使用SpringApplication,大多数应用程序设置都有硬编码的默认值,如要使用的配置文件和属性文件等。您需要查看此类的代码才能理解这一点。

With SpringApplicationBuilder, you can simply change few of these application default settings before application starts even though most of these settings have sensible default values. So with few lines of code, you can build a different applications with different settings for different purposes ( embedded deployment, external deployment , testing etc ) while your actual underlying business logic remains same.

使用SpringApplicationBuilder,您可以在应用程序启动之前简单地更改其中的一些应用程序默认设置,即使这些设置中的大多数都有合理的默认值。因此,只需几行代码,您就可以为不同目的(嵌入式部署、外部部署、测试等)构建具有不同设置的不同应用程序,同时您的实际底层业务逻辑保持不变。