20

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?

Ole
  • 41,793
  • 59
  • 191
  • 359
  • 1
    Did you read the Javadocs on the class? – chrylis -cautiouslyoptimistic- Aug 28 '17 at 03:33
  • 1
    I did. It looks as if they are similar classes, but SpringApplicationBuilder offers more control as far as profiles, etc. go. In this case though it looks as if the ZuulApplication probably would have been fine with just SpringApplication, but the author chose to use a SpringApplicationBuilder ...? – Ole Aug 28 '17 at 04:20
  • 1
    In this case, it does look like the use is unnecessary. The `web` parameter is normally autodetermined based on whether the proper dependencies are available; explicitly setting it to `true` will either make startup fail or will be redundant. – chrylis -cautiouslyoptimistic- Aug 28 '17 at 08:50
  • Indeed - that's what I thought was strange ... – Ole Aug 28 '17 at 13:54

3 Answers3

12

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

Daniel C.
  • 5,418
  • 3
  • 23
  • 26
10

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.

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
9

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

Saravana
  • 12,647
  • 2
  • 39
  • 57