In this post, we’ll explore how to create a custom banner in a Spring Boot application.

· Overview
∘ Why customize the Spring Boot Banner?
· Customizing the Banner
∘ Using banner.txt
∘ Using a Custom Banner Class
∘ Disabling the Spring Boot Banner
· Conclusion
· References


Overview

When a Spring Boot application starts, it prints a banner, a visual representation, or text (often with ASCII graphics or branding information). The banner is a way to provide a more engaging and informative startup experience for developers and users such as the application name, version, or any other relevant data.

By default, Spring Boot includes a simple banner displaying the Spring logo and the version number. Here’s an example of what the default banner looks like:

The default Spring boot banner

Why customize the Spring Boot Banner?

  • Branding: You can use the banner to display your company’s name, application name, or logo in ASCII art. This can help make your application startup more engaging.
  • Version Information: Including version numbers or other metadata (like build number, commit hash, or environment information) in the banner can provide helpful context when deploying or debugging applications.
  • Customizing the Startup Message: The banner is a good way to show developers or users a custom greeting or motivational message when the application starts. This could be a way of displaying build info, or simply a fun message to brighten the development environment.

Customizing the Banner

Spring Boot provides several ways to customize the banner.

Using banner.txt

The banner that is printed on start-up can be changed by adding a banner.txt file to your classpath or by setting the spring.banner.location property to the location of such a file. If the file has an encoding other than UTF-8, you can set spring.banner.charset.

Example:

  • Create a banner.txt file in src/main/resources/ directory.
  • This file can contain any ASCII art, text, or any message you want to show when the application starts.
${Ansi.RED}.______     ______     ______   .___________. ${Ansi.GREEN}  .___________. _______   ______  __    __
${Ansi.RED}| _ \ / __ \ / __ \ | | ${Ansi.GREEN} | || ___
_| / || | | |
${Ansi.RED}| |_
) | | | | | | | | | `---| |----` ${Ansi.GREEN} `---| |----`| |__ | ,----'| |__| |
${Ansi.RED}| _ < | | | | | | | | | | ${Ansi.GREEN} | | | __| | | | __ |
${Ansi.RED}| |_
) | | `--' | | `--' | | | ${Ansi.GREEN} | | | |____ | `----.| | | |
${Ansi.RED}|______/ \______/ \______/ |__| ${Ansi.GREEN} |__| |_______| \______||__| |__|
${Ansi.BLUE}oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
${AnsiColor.BRIGHT_BLUE} :: Welcome to My Spring Boot Application!
${AnsiColor.GREEN} :: Have a wonderful day! 🌷
${AnsiColor.YELLOW} Powered by Spring Boot ${spring-boot.formatted-version} ::${AnsiColor.DEFAULT}

You can use online ASCII art generators or tools like https://devops.datenkollektiv.de/banner.txt/index.html to generate text-based banners.

Output:

Let’s start the application, we can see our custom banner.

if we want to choose any other location or another name for the banner, we need to set the spring.banner.location property in the application.properties file:

spring.banner.location=classpath:/your_custom_path/bannername.txt

Using a Custom Banner Class

In this case, we want to create a more complex or dynamic banner. The SpringApplication.setBanner(…​) method can be used if we want to generate a banner programmatically. We need to implement the org.springframework.boot.Banner interface and override the printBanner method. This method is responsible for printing the banner.

public class CustomAnsiBanner implements Banner {

    @Override
    public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
        // ANSI escape codes for colors
        String red = "\u001B[31m";
        String green = "\u001B[32m";
        String blue = "\u001B[34m";
        String brightBlue = "\u001B[1;34m"; // Bright blue
        String yellow = "\u001B[33m";
        String reset = "\u001B[0m";

        // Custom banner text
        out.println(red + "______     ______     ______   .___________. " + green + "  .___________. _______   ______  __    __" + reset);
        out.println(red + "|   _  \\   /  __  \\   /  __  \\  |           | " + green + "  |           ||   ____| /      ||  |  |  |" + reset);
        out.println(red + "|  |_)  | |  |  |  | |  |  |  | `---|  |----` " + green + "  `---|  |----`|  |__   |  ,----'|  |__|  |" + reset);
        out.println(red + "|   _  <  |  |  |  | |  |  |  |     |  |      " + green + "      |  |     |   __|  |  |     |   __   |" + reset);
        out.println(red + "|  |_)  | |  `--'  | |  `--'  |     |  |      " + green + "      |  |     |  |____ |  `----.|  |  |  |" + reset);
        out.println(red + "|______/   \\______/   \\______/      |__|      " + green + "      |__|     |_______| \\______||__|  |__|" + reset);
        out.println(blue + "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo" + reset);
        out.println(brightBlue + "                :: Welcome to My Spring Boot Application!" + reset);
        out.println(green + "                     ::        Have a wonderful day! 🌷" + reset);
        out.println(yellow + "                  Powered by Spring Boot " + environment.getProperty("spring-boot.version") + " ::" + reset);
    }
}

Next, in the main application class, we register the custom banner.

@SpringBootApplication
public class SpringBootCustomBannerApplication {

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

}

Disabling the Spring Boot Banner

If you don’t want the default Spring Boot banner or a custom banner to appear when the application starts, you can easily disable it.

There are two ways to disable the banner in a Spring Boot application:

  • Using application.properties:
spring.main.banner-mode=off
  • Disabling the Banner Programmatically
@SpringBootApplication
public class MyApplication {

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

}
  • Disabling the Banner in the Command Line
java -jar your-application.jar --spring.main.banner-mode=off

Conclusion

In this post, we explored creating a custom banner in a Spring Boot application.

The complete source code is available on GitHub.

Thank you for Reading !! See you in the next story.

References

👉 Link to Medium blog

Related Posts