Spring Boot - SpringApplicationBuilder Class Example

In this article, we will discuss the usage of SpringApplicationBuilder Class in Spring boot applications.
The SpringApplicationBuilder class provides a fluent API and is a builder for the SpringApplication and ApplicationContext instances. It also provides hierarchy support.

SpringApplicationBuilder Class Examples

In below example, we are using SpringApplicationBuilder class to build the Spring application. We turn off the banner and the startup information using SpringApplicationBuilder class APIs:
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(Application.class);
        app.setBannerMode(Mode.OFF);
        app.run(args);
    }
}
You can have a hierarchy when you’re creating a Spring application and you can create this with the SpringApplicationBuilder. A simple example of a context hierarchy:
import org.springframework.boot.Banner.Mode;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class)
            .child(MyConfig.class)
            .run(args);
    }
}
Note that if you have a web configuration, make sure it’s being declared as a child. All the web configuration must depend on the main Spring context, which is why it needs to be declared as a child. Also, parent and children must share the same org.springframework.core.Environment interface.
Let's look at how to specify whether to log the info at startup or not; by default, this is set to true:
new SpringApplicationBuilder(Application.class)
 .logStartupInfo(false)
 .run(args); 
Another common use case is setting active profiles and default properties to set up the environment for an application:
 new SpringApplicationBuilder(Application.class).profiles("server")
                .properties("transport=local").run(args);
You can attach listeners for some of the ApplicationEvent events:
@SpringBootApplication
public class SpringBootSimpleApplication {
    Logger log = LoggerFactory.getLogger(SpringBootSimpleApplication.class);
    public static void main(String[] args) {
        new SpringApplicationBuilder(SpringBootSimpleApplication.class)
            .listeners(new ApplicationListener < ApplicationEvent > () {
                @Override
                public void onApplicationEvent(ApplicationEvent event) {
                    log.info("#### > " + event.getClass().getCanonicalName());
                }
            })
            .run(args);
    }
}
When you run your application, you should see at least the following output:
...
#### > org.springframework.boot.context.event.ApplicationPreparedEvent
...
#### > org.springframework.context.event.ContextRefreshedEvent
#### > org.springframework.boot.context.event.ApplicationReadyEvent
...
#### > org.springframework.context.event.ContextClosedEvent
... 
Let's look at one more example, you can programmatically specify whether the spring boot application is a web app or standalone like:
new SpringApplicationBuilder(Application.class)
 .web(false)
 .run(args); 
SpringApplicationBuilder class provides many more APIs to work programmatically with SpringApplication and ApplicationContext instances at https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/builder/SpringApplicationBuilder.html

References

Comments