When do we use SpringApplicationBuilder?

I am reading a tutorial on

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

In most cases, I see this to launch the Spring boot application:

 SpringApplication.run(Application.class, args); 

This is the first time I have seen SpringApplicationBuilder. When should we use this as a whole?

+5
source share
3 answers

In our application, we used SpringApplicationBuilder in the starter application. starter is a simple application that programmatically runs actual instances of the application.

The number of running processes and the web / standalone process type will be passed as an argument to the starter application based on the arguments that the application instances will be launched. we used -w to run as a state management web application.

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

There is another way to do the same.

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

We can also customize banner registrars with SpringApplicationBuilder .

read the document for greater benefit

+2
source

Let's say that you need to solve a problem when you need to work with several databases or structures, and each of them should be isolated from the other, in this case I would use the SpringApplicationBuilder approach, because each domain can be isolated by creating a parent and a child context, and there is no need to mix different problems with the domain, for example, you can have the configuration Application1 and Application2, each of which has its own domains, controllers and repositories, but you do not want to mix all this complexity and instead, you can create two different configurations using SpringApplicationBuilder

  SpringApplicationBuilder appBuilder = new SpringApplicationBuilder() .sources(Parent.class); appBuilder.child(Application1.class).run(args); appBuilder.child(Application2.class).run(args); 

More info: Post with SpringApplicationBuilder example , Java Doc from SpringBuilder and Another SpringApplicationBuilder usage example

+2
source

One common use case I came across is when you want a traditional deployment war file to be deployed to Weblogic, etc. Traditional deployment

With SpringApplication most application settings have hardcoded defaults, such as profiles and property files to use, etc. You need to look at this class code to understand this.

With SpringApplicationBuilder you can simply change some of these default settings for the application before launching the application, even if most of these settings have reasonable default values. Thus, with a few lines of code, you can create different applications with different settings for different purposes (built-in deployment, external deployment, testing, etc.), while your actual basic business logic remains the same.

+2
source

Source: https://habr.com/ru/post/1271285/


All Articles