Customizing The Spring Boot Banner

Spring boot shows a banner during application startup. The Spring Boot application prints this banner on the application console/log file. We can replace the default banner with a custom banner. Customizing the spring boot banner is an easy process.

In the article, we will learn how to customize the Spring Boot application banner.

Technologies used in this article are:

  • Spring Boot version : 2.3.0.RELEASE
  • Java version 1.8

Table of Contents

Customizing spring boot startup banner

Create a Spring Boot application. Let us add the spring-boot-starter-web dependency to the applications pom.xml file.

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

Start the Spring Boot application. We will get the default Spring Boot banner displayed on the console, as shown in the below image.

customizing spring boot banner

Displaying Custom Spring Boot Banner Text

Create a text file with the name banner.txt under the directory /src/main/resources.

spring boot custom banner example

Add the banner content to the created text file. I have added a banner text that prints ASB Notebook.

${AnsiColor.BRIGHT_BLUE}     /\    ${AnsiColor.RED}  / ____| ${AnsiColor.YELLOW}|  _ \    ${AnsiColor.BRIGHT_MAGENTA}| \ | |         | |          ${AnsiColor.CYAN}| |                     | |   
${AnsiColor.BRIGHT_BLUE}    /  \   ${AnsiColor.RED} | (___   ${AnsiColor.YELLOW}| |_) |   ${AnsiColor.BRIGHT_MAGENTA}|  \| |   ___   | |_    ___  ${AnsiColor.CYAN}| |__     ___     ___   | | __
${AnsiColor.BRIGHT_BLUE}   / /\ \  ${AnsiColor.RED}  \___ \  ${AnsiColor.YELLOW}|  _ <    ${AnsiColor.BRIGHT_MAGENTA}| . ` |  / _ \  | __|  / _ \ ${AnsiColor.CYAN}| '_ \   / _ \   / _ \  | |/ /
${AnsiColor.BRIGHT_BLUE}  / ____ \ ${AnsiColor.RED}  ____) | ${AnsiColor.YELLOW}| |_) |   ${AnsiColor.BRIGHT_MAGENTA}| |\  | | (_) | | |_  |  __/ ${AnsiColor.CYAN}| |_) | | (_) | | (_) | |   < 
${AnsiColor.BRIGHT_BLUE} /_/    \_\${AnsiColor.RED} |_____/  ${AnsiColor.YELLOW}|____/    ${AnsiColor.BRIGHT_MAGENTA}|_| \_|  \___/   \__|  \___| ${AnsiColor.CYAN}|_.__/   \___/   \___/  |_|\_\
${AnsiColor.RED}Application version : ${application.version} 
${AnsiColor.GREEN}Spring Boot Version : ${spring-boot.version} 
${AnsiColor.BLUE}Application Title : ${application.title}
${AnsiColor.BLACK}

A few of the points to notice here are:

  • We can use ANSI colors to change the text color. The supported colors list is available here.
  • application.version: The declared version number of the application in the MANIFEST.MF.
  • application.title: The declared application title of the application on the MANIFEST.MF.
  • spring-boot.version: The Spring Boot version used on the application.

If we run the application, We will get the following custom banner.

customizing spring boot banner

The application may not print all the properties’ values when we are running the application on the IDE.

The packaged application jar will print all the details, as shown in the below image.

customizing spring boot banner

Using Image as Spring Boot banner

We can use .gif, .png, or .jpg images as custom Spring Boot application banners. The image is converted into an ASCII art representation and printed above the text banner.

Add an image as shown in the below image. We have added a my_banner.jpg image under the /src/main/resources directory.

spring boot image banner

We can use the following spring Boot properties to configure the characteristics of the banner image.

spring.banner.image.location=classpath:my_banner.jpg
spring.banner.image.pixelmode=BLOCK
spring.banner.image.bitdepth=4
spring.banner.image.invert=true

Once we start the application, we will get the image banner, as shown in the below image.

Spring boot banner custom image example

Creating the banner programmatically

We can also create the application banner text programmatically.

In the below code snippet, we are calling the setBanner() method of the SpringApplication class. We have also implemented the printBanner() method of the Banner interface to print the custom banner.

@SpringBootApplication
public class SpringBootCustomBannerExampleApplication {
    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(SpringBootCustomBannerExampleApplication.class);
        application.setBanner((env, source, out) -> {
            out.println("=============================");
            out.print("ASB Notebook\n");
            out.println("=============================");
        });
        application.run(args);
    }
}

Once we run the application, we will get the custom banner, as shown in the below image.

spring boot create custom banner programatically

Disabling the Spring Boot banner

We can disable the application startup banner by calling the setBannerMode() method of SpringApplication class and passing the value off.

application.setBannerMode(Banner.Mode.OFF);

We can also set the below Spring Boot application property with value off to disable the application startup banner.

spring.main.banner-mode=off

Conclusion

In this article, we learned how to add custom text and image banners to the Spring Boot application.

We also learned how to disable the banner and set the custom banner programmatically.

Complete Example code is available on GitHub.

Customizing The Spring Boot Banner
Scroll to top

Discover more from ASB Notebook

Subscribe now to keep reading and get access to the full archive.

Continue reading