Learnitweb

Custom banner in Spring Boot

1. Introduction

Whenever Spring Boot application is started, a banner is printed. The banner looks like the following:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.6.0)

This startup banner can be customized. We can use custom text as well as image.

2. How to customize banner in Spring Boot?

There are two ways to change the banner:

  1. Adding banner.txt file in classpath. In Spring Boot project, banner.txt file can be placed in src/main/resources. The file banner.txt contains the banner to be displayed.
  2. Setting spring.banner.location property to the location of banner file.

3. Using image as a banner

An image file can also be used as a banner by following two ways:

  1. Adding a banner.gif, banner.jpg, or banner.png image file to your classpath. In Spring Boot project, image file can be placed in src/main/resources. The file banner.txt contains the banner to be displayed.
  2. Setting spring.banner.image.location property to the location of the banner image.

It is recommended to use text file as banner because images are converted into an ASCII art representation and printed above any text banner. If a complex and large image is used, the startup time of application may increase.

4. Placeholders in banner.txt

The banner.txt file can contain certain placeholders. These placeholders represent specific information which you may want to print.

${application.version}The version number of your application, as declared in MANIFEST.MF.
${application.formatted-version}The version number of your application, as declared in MANIFEST.MF. The value is formatted for display, enclosed within brackets and prefixed with v. For example (v2.1).
${spring-boot.version}The Spring Boot version.
${spring-boot.formatted-version}The Spring Boot version, formatted for display, enclosed within brackets and prefixed with v. For example, (2.6.1).
${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME})Here, NAME is the name of ANSI escape code. For example, ${Ansi.RED}.
${application.title}Application title as specified in MANIFEST.MF.

5. Example of custom banner in Spring Boot

Create banner.txt file having following content and place in resources folder of your Spring Boot application.

${Ansi.BLUE}========================================
${Ansi.RED} This is custom banner
${Ansi.BLUE}========================================
${Ansi.GREEN} Spring Boot version: ${spring-boot.version}
${Ansi.GREEN} Spring Boot formatted version: ${spring-boot.formatted-version}
${Ansi.BLUE}========================================

When the application is started the output would look like the following:

custom banner in spring boot

6. Other properties for banner

There are few other properties related to banner.

NameDescriptionDefault Value
spring.banner.charsetBanner file encodingUTF-8
spring.banner.image.bitdepthBit depth to use for ANSI colors. Supported values are 4 or 8.4
spring.banner.image.heightHeight of the banner image in chars
spring.banner.image.invertWhether images should be inverted for dark terminal themes.false
spring.banner.image.marginLeft hand image margin in chars.2
spring.banner.image.pixelmodePixel mode to use when rendering the image.TEXT
spring.banner.image.widthWidth of the banner image in chars.76

There is one property spring.main.banner-mode which can have value as console, log or off. These values mean you want to show banner on System.out, configured logger or not show at all.

7. Conclusion

In this tutorial, we discussed how to customize banner in Spring Boot and how to use placeholders in banner. The custom banner is used when you want to show your product information when you ship your product.