Custom Banners in Spring Boot

Spring Boot applications display a default banner text when the application starts. This banner can be customized to display custom messages, ASCII art, or any other information you want to display at startup. In this tutorial, we will learn how to customize the Spring Boot startup banner.

Prerequisites

  • JDK 17 or later
  • Maven or Gradle
  • IDE (IntelliJ IDEA, Eclipse, etc.)

Step 1: Set Up a Spring Boot Project

1.1 Create a New Spring Boot Project

Use Spring Initializr to create a new project with the following dependencies:

  • Spring Web

Download and unzip the project, then open it in your IDE.

Step 2: Create a Custom Banner

2.1 Create a banner.txt File

Spring Boot allows you to customize the banner by creating a banner.txt file and placing it in the src/main/resources directory.

Here is an example of a custom banner.txt file with ASCII art:

  ____             _    ____              _
 | __ )  __ _  ___| | _| __ )  ___   ___ | | __
 |  _ \ / _` |/ __| |/ /  _ \ / _ \ / _ \| |/ /
 | |_) | (_| | (__|   <| |_) | (_) | (_) |   <
 |____/ \__,_|\___|_|\_\____/ \___/ \___/|_|\_\

:: Spring Boot ::  (v2.4.5)

Explanation:

  • The content of the banner.txt file will be displayed as the startup banner when the Spring Boot application starts.
  • You can use any ASCII art generator to create custom designs for your banner.

2.2 Customize Banner Text

You can include Spring Boot properties in your banner.txt file by using placeholders:

  ____             _    ____              _
 | __ )  __ _  ___| | _| __ )  ___   ___ | | __
 |  _ \ / _` |/ __| |/ /  _ \ / _ \ / _ \| |/ /
 | |_) | (_| | (__|   <| |_) | (_) | (_) |   <
 |____/ \__,_|\___|_|\_\____/ \___/ \___/|_|\_\

:: ${spring-boot.version} ::
Application Name: ${application.name}
Application Version: ${application.version}

2.3 Define Custom Properties

Define the custom properties used in the banner in the application.properties file:

application.name=My Spring Boot Application
application.version=1.0.0

Explanation:

  • ${spring-boot.version}: Displays the Spring Boot version.
  • ${application.name}: Displays the custom application name defined in the application.properties.
  • ${application.version}: Displays the custom application version defined in the application.properties.

Step 3: Using Banner Interfaces

Spring Boot also allows you to programmatically customize the banner using Banner interfaces.

3.1 Implement Custom Banner

Create a custom banner class by implementing the Banner interface:

package com.example.demo;

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.core.env.Environment;

import java.io.PrintStream;

public class CustomBanner implements Banner {

    @Override
    public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
        out.println("  ____             _    ____              _    ");
        out.println(" | __ )  __ _  ___| | _| __ )  ___   ___ | | __");
        out.println(" |  _ \\ / _` |/ __| |/ /  _ \\ / _ \\ / _ \\| |/ /");
        out.println(" | |_) | (_| | (__|   <| |_) | (_) | (_) |   < ");
        out.println(" |____/ \\__,_|\\___|_|\\_\\____/ \\___/ \\___/|_|\\_\\");
        out.println("                                              ");
        out.printf(":: %s ::%n", environment.getProperty("spring-boot.version"));
        out.printf("Application Name: %s%n", environment.getProperty("application.name"));
        out.printf("Application Version: %s%n", environment.getProperty("application.version"));
    }
}

3.2 Register Custom Banner

Register the custom banner in the main application class:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(DemoApplication.class);
        app.setBanner(new CustomBanner());
        app.run(args);
    }
}

Explanation:

  • The CustomBanner class implements the Banner interface and overrides the printBanner method to print custom banner content.
  • The app.setBanner(new CustomBanner()) line registers the custom banner in the main application class.

Step 4: Disabling the Banner

If you want to disable the banner, you can do so by setting a property in the application.properties file:

spring.main.banner-mode=off

Alternatively, you can disable the banner programmatically:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(DemoApplication.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);
    }
}

Explanation:

  • spring.main.banner-mode=off: Disables the banner by setting the banner-mode property to off.
  • app.setBannerMode(Banner.Mode.OFF): Disables the banner programmatically by setting the Banner.Mode.OFF.

Step 5: Running and Testing the Application

5.1 Run the Application

Run the Spring Boot application using your IDE or the command line:

./mvnw spring-boot:run

5.2 Test the Custom Banner

Check the console output when the application starts. You should see the custom banner displayed, as defined in the banner.txt file or the CustomBanner class.

Conclusion

In this tutorial, you have learned how to customize the Spring Boot startup banner using different methods. By creating a custom banner.txt file, using properties placeholders, implementing the Banner interface, and registering a custom banner programmatically, you can provide a personalized startup experience for your Spring Boot applications. You also learned how to disable the banner if it is not needed.


Comments