SpringApplicationBuilder tutorial

SpringApplicationBuilder tutorial shows how to use SpringApplicationBuilder to create a simple Spring Boot application.

Spring is a popular Java application framework for creating enterprise applications. Spring Boot is an evolution of Spring framework which helps create stand-alone, production-grade Spring based applications with minimal effort.


SpringApplication is a class to bootstrap a Spring application from a Java main method. It creates an appropriate ApplicationContext instance (depending on the classpath), registers a CommandLinePropertySource to expose command line arguments as Spring properties, refreshes the application context, loading all singleton beans, and triggers any CommandLineRunner beans.


SpringApplicationBuilder is a builder for SpringApplication and ApplicationContext instances with convenient fluent API and context hierarchy support.

Spring Boot example

The following application is a simple Spring Boot console application which uses SpringApplicationBuilder to set up a Spring Boot application.

The application takes an argument from the user; it expects a full URL of a website and returns its title.

$ tree
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           ├── Application.java
    │   │           └── MyRunner.java
    │   └── resources
    │       └── application.properties
    └── test
        └── java

This is the project structure.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 



Spring Boot starters are a set of convenient dependency descriptors which greatly simplify Maven configuration. The spring-boot-starter-parent has some common configurations for a Spring Boot application. The spring-boot-starter is the core Spring starter. The jsoup dependency is for the JSoup library.

The spring-boot-maven-plugin provides Spring Boot support in Maven, allowing us to package executable JAR or WAR archives. Its spring-boot:run goal runs the Spring Boot application.


In the application.properties file we write various configuration settings of a Spring Boot application. Here we set the logging level for Spring framework to ERROR.

package com.zetcode;

import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

public class MyRunner implements ApplicationRunner {

    public void run(ApplicationArguments args) throws Exception {

        if (!args.containsOption("website")) {

            System.err.println("no website specified");
        } else {

            List<String> vals = args.getOptionValues("website");
            String url = vals.get(0);

            Document doc = Jsoup.connect(url).get();
            String title = doc.title();
            System.out.printf("The title is: %s%n", title);

After the Spring application is loaded, any bean that implements ApplicationRunner is executed.

if (!args.containsOption("website")) {

We check if there is a --website option specified on the command line.

List<String> vals = args.getOptionValues("website");
String url = vals.get(0);

We get the value of the option.

Document doc = Jsoup.connect(url).get();
String title = doc.title();
System.out.printf("The title is: %s%n", title);

With JSoup, we get the title of the specified website.

package com.zetcode;

import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

public class Application {

    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class)

Application is the entry point which sets up Spring Boot application. The @SpringBootApplication annotation enables auto-configuration and component scanning.

new SpringApplicationBuilder(Application.class)

The SpringApplicationBuilder is used to build the Spring application. We turn off the banner and the startup information.

$ mvn -q spring-boot:run -Drun.arguments=--website=http://www.something.com
The title is: Something.

This is the output. The command line arguments are passed with run.arguments.

In this tutorial, we have covered SpringApplicationBuilder.

You might be interested in JSoup tutorial, Spring Boot Environment tutorial. Spring Boot PostgreSQL tutorial, Spring Boot MySQL tutorial, or Spring Boot @PostConstruct.