Ole Ole - 2 months ago 20
Java Question

When do we use a SpringApplicationBuilder?

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

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


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

SpringApplication.run(Application.class, args);


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

Answer Source

In our application we have used SpringApplicationBuilder in the starter application. starter is an simple application which will start the actual application instances programatically.

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

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 customise banner, loggers with SpringApplicationBuilder.

read the doc for more usage