Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 977
Ole Avatar asked Aug 28 '17 03:08

Ole


3 Answers

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

like image 24
Saravana Avatar answered Nov 18 '22 23:11

Saravana


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

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.

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.

like image 111
Sabir Khan Avatar answered Nov 18 '22 23:11

Sabir Khan


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 SpringApplicationBuilder approach, 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 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 SpringBuilder and Other example of how use SpringApplicationBuilder

like image 10
Daniel C. Avatar answered Nov 18 '22 21:11

Daniel C.