Ebooks

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

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.

$ tree
.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── zetcode
    │   │           ├── Application.java
    │   │           └── MyRunner.java
    │   └── resources
    │       └── application.properties
    └── 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>SpringBootAppEx</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.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.11.2</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.

application.properties
logging.level.org.springframework=ERROR

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.

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.

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