Custom Banners in Spring Boot

How to Use Custom Banners in Spring Boot

When we start our Spring Boot application, it comes up with a default Banner, in this post we will discuss how to use Custom Banners in Spring Boot application.

 

Introduction

It’s highly likely that you want to release your own product/application based on the Spring Boot and want to display your own custom banner in place of default Spring Boot Banner.By default Spring

By default, Spring Boot application will display the following banner on startup

<span class="pun">.</span><span class="pln">   ____          _            __ _ _
 </span><span class="pun">/</span><span class="pln">\\ </span><span class="pun">/</span><span class="pln"> ___</span><span class="str">'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '</span><span class="pln">_ </span><span class="pun">|</span> <span class="str">'_| | '</span><span class="pln">_ \/ _</span><span class="str">` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.7.RELEASE)</span><

 

1. Creating Custom Banner

In order to start, we need to create a custom banner which will be used to display on the application startup time. I will be using  Spring Boot Banner Generator to upload an image for generating ANSI character in the plain text. You can always generate it manually if you want :).

For this tutorial, I will be using  Spring Logo logo from the Spring IO site. 

 

2. Using The Custom Banner

In the above section, we have created a custom banner and it’s time to start using this custom banner. We will be creating a file banner.txt under the src/main/resources folder and will paste content in this file.

Spring Boot by default will pick content from the banner.txt file, in case it will find a banner.txt in our project classpath (resources folder of our application), it will pick custom banner content and will display it on the startup.

In case we want Spring Boot to pick banner content from other location and not from the default banner.txt, we can customize that by setting banner.location to the location of such a file

banner.location=classpath:/path/to/banner/custom-banner.txt

Here is the output when we run our application with new content in the banner.txt file

Custom Banners in Spring Boot
Custom Banners in Spring Boot

 

2.1 Using Image for Custom Banner

We can even use the image as a custom banner for our Spring Boot application, We can add banner.gif, banner.jpg or banner.png image file to your classpath and Spring Boot will automatically pick this image as a startup banner. Please note that we need to name these banner images as a banner.extension (e.g. banner.jpg). 

You can use banner.image.location property to set a custom location for our banner image in the application.properties file, we can also use some additional properties to customize our banner


banner.image.location=classpath:banner.gif # Banner image file location (jpg/png can also be used).
banner.image.width= # Width of the banner image in chars (default 76)
banner.image.height= # Height of the banner image in chars (default based on image height)
banner.image.margin= # Left hand image margin in chars (default 2)
banner.image.invert= # If images should be inverted for dark terminal themes (default false)

Images will be converted into an ASCII art representation before getting printed on the startup which can add a lot of time on startup in case we have a complex image.It is recommended to use text format for a Custom Banners in Spring Boot.

If you want you can use SpringApplication.setBanner(… ) method to set custom banner programmatically but in my opinion, this is not preferred way and you need to implement your own printBanner() provided under org.springframework.boot.Banner interface. 

 

Summary 

In this short post, we learned how to use a Custom Banners in Spring Boot. We explored the option to create a custom banner using a banner.txt file or placing your custom image in your classpath.