ZetCode

SpringApplicationBuilder tutorial

last modified July 6, 2020

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

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

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.

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

This is the project structure.

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
         http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zetcode</groupId>
    <artifactId>SpringBootApplicationBuilder</artifactId>
    <version>1.0-SNAPSHOT</version>

    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.12.1</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

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.

com/zetcode/MyRunner.java
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;

@Component
public class MyRunner implements ApplicationRunner {

    @Override
    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.

com/zetcode/Application.java
package com.zetcode;

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

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        new SpringApplicationBuilder(Application.class)
                .bannerMode(Banner.Mode.OFF)
                .logStartupInfo(false)
                .build()
                .run(args);
    }
}

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

new SpringApplicationBuilder(Application.class)
        .bannerMode(Banner.Mode.OFF)
        .logStartupInfo(false)
        .build()
        .run(args);

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

$ mvn -q spring-boot:run -Dspring-boot.run.arguments=--website=http://webcode.me
The title is: My html page

This is the output. The command line arguments are passed with the spring-boot.run.arguments. The -q (for quiet) is a Maven option that turns of Maven messages.

In this tutorial, we have covered SpringApplicationBuilder.

List Spring Boot tutorials.