Custom Banners in Spring Boot

author : Sai K

Spring Boot allows you to customize the startup banner that is displayed when the application starts. By default,

Spring Boot shows a simple ASCII art banner. This tutorial will guide you through the steps to create and use a

custom banner in your Spring Boot application.


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.

1.2 Configure application.properties

(Optional) Set up the application properties for your project. This file is located in the src/main/resources

directory. You can customize the banner properties here if needed.


# src/main/resources/application.properties

# Server port
server.port=8080

# Banner mode (console, log, or off)
spring.main.banner-mode=console

Explanation:

  • spring.main.banner-mode: Specifies where the banner is displayed (console, log, or off).
  • Step 2: Create a Custom Banner

    Spring Boot looks for a banner.txt file in the src/main/resources directory to display as the startup banner. You

    can create your own ASCII art banner or use an online tool to generate one.

    2.1 Create banner.txt

    Create a file named banner.txt in the src/main/resources directory and add your custom ASCII art banner. You

    can use an online ASCII art generator like TAAG to create your banner

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

    You can also add Spring Boot placeholders in your banner for dynamic content:

    • ${application.version}: Displays the application version.

    • ${spring-boot.version}: Displays the Spring Boot version.

    Example:

    
        ____                  _        ____              _
        |  _ \  ___   __ _  __| | ___  | __ )  ___   ___ | |_
        | | | |/ _ \ / _` |/ _` |/ _ \ |  _ \ / _ \ / _ \| __|
        | |_| | (_) | (_| | (_| |  __/ | |_) | (_) | (_) | |_
        |____/ \___/ \__,_|\__,_|\___| |____/ \___/ \___/ \__|
       
        :: Spring Boot ::  (v${spring-boot.version})
    

    Step 3: Customize Banner Using Code

    If you want to customize the banner using code, you can implement the Banner interface.

    3.1 Create a Custom Banner Class

    Create a new class named CustomBanner in the com.example.demo package (or any other package).

    
    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.println(" :: Custom Spring Boot Banner :: ");
        }
    }
    

    3.2 Register the Custom Banner

    Modify the main method in your DemoApplication class to register the custom banner.

    
    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);
        }
    }
    

    Step 4: Running and Testing the Application

    4.1 Run the Application

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

    ./mvnw spring-boot:run

    4.2 Check the Custom Banner

    When the application starts, you should see your custom banner displayed in the console or log, depending on

    your configuration.


    Conclusion

    In this tutorial, you have learned how to customize the startup banner in a Spring Boot application. We covered:

    • Setting up a Spring Boot project.

    • Creating a custom ASCII art banner in a banner.txt file.

    • Adding dynamic content to the banner using placeholders.

    • Implementing a custom banner programmatically using the Banner interface.

    • Registering and displaying the custom banner.

    By following these steps, you can create and display custom banners in your Spring Boot applications to provide

    a unique and personalized startup experience.