Custom Banners In Spring Boot

Introduction

As soon as you run your spring boot application, it comes up with a default banner. Have you ever thought that you can easily change that banner with very little configuration ? Well, then this article is for you. Here we will see how to customize the spring boot banner to load at application start up. This becomes useful when you want to release your own product/application based on spring boot and you want to display your own custom banner.

By-default, Spring Boot comes up with the following banner on application start-up.

What you will learn ?
  • Using banner.txt file in classpath (resource folder of our application)
  • Using image file as banner
  • Using both “banner.txt” and “banner.png” at the same time.
  • Configure Banner location in Application Property File
  • Banner Variables
  • Generating banner programmatically
  • Turn – Off the banner
Bootstrap you application through SPRING INITIALIZR

Create your Spring-Boot project with the help of Spring Initializr. Provide the Project Metadata as shown below and default Dependencies are required, so no dependencies has been chosen.

Click on the Generate Project button to download the project, extract it to any location and import in your favorite IDE. I am using Eclipse Oxygen for the same.

Standard Maven Project Structure

Following is the complete project structure that we will be developing step-by-step.

Project Dependencies – pom.xml

Creating Custom Banner

There are different ways we can create customized banner. Default spring boot banner can be changed in the following ways.

Using banner.txt  file in classpath (resource folder of our application)

First-of-all we will keep ASCII – art representation of our banner ready. Create Your Own Spring Boot Banner, click on the given link and generate a banner of your choice.

Click on “Text to ASCII art Generator” and do the following customization to generate a banner for yourself.

Now click on “Select & Copy” button to copy the banner that suits you. Create a file named “banner.txt” in the “src/main/resources” folder and paste the banner content into this file. Remember that “banner.txt” is the default expected file name that spring boot will pick up from classpath. Run the application and see that default banner will be replaced with your banner.

Using image file as banner

In addition to “banner.txt” text file, we can also use image file as banner. Simply add a banner.gif or banner.jpg or banner.png image file to your classpath. Remember that “banner.gif | banner.jpg | banner.png” is the expected image file name that spring boot will pick by default from classpath. I added “banner.png” containing logo of our blog in the classpath.

You can even set the image width and height in the application.properties file as follows.

[su_box title=”application.properties” box_color=”#faf62b” title_color=”#151111″]
spring.banner.image.width=25
spring.banner.image.height=20
[/su_box]

Test it by running the application and default banner will be replaced with the following.

Using both “banner.txt” and “banner.png” at the same time.

We can even use both “banner.txt” and “banner.png” at the same time in our classpath. Image file will be shown before the text file as follows.

Configure Banner location in Application Property File

We can configure banner, both text and image in the application property file. In this way we can choose to load the text and image file from any other location with some other name. Spring Boot provides the following property to configure location or name in the application.properties file.

spring.banner.location=classpath:/path/to/banner/bannername.txt
spring.banner.image.location=classpath:/path/to/banner/bannername.gif

Before configuring this, let us know about Banner Variables.

Banner Variables

If you want to display some additional information with banner on application start-up, you can do some by using banner variables. They can be used as placeholders inside your banner.txt. Click “Banner Variables” to know more about it from the spring boot reference document.

[su_box title=”Note” box_color=”#faf62b” title_color=”#151111″]
Oracle documentation defines the manifest files as

The manifest is a special file that can contain information about the files packaged in a JAR file. By tailoring this “meta” information that the manifest contains, you enable the JAR file to serve a variety of purposes. When you create a JAR file, a default manifest is created automatically.
[/su_box]

Now lets see the example using custom location and banner variables.

Create a folder “banner” or any name of your choice inside “src/main/resource“. Also create text file for banner called “mybanner.txt” or any name of your choice inside banner folder as shown below.

Contents of mybanner.txt

Provide the following property in “application.properties”.

spring.banner.location=classpath:/banner/mybanner.txt

If you run this application, all the placeholders don’t get resolved as this will read some property from MANIFEST.MF file which looks like the following.

Manifest-Version: 1.0
Implementation-Title: spring-boot-custom-banner
Implementation-Version: 0.0.1-SNAPSHOT
Built-By: Chandra Mani
Implementation-Vendor-Id: com.dev.codeadda.banner
Spring-Boot-Version: 2.1.2.RELEASE
Main-Class: org.springframework.boot.loader.JarLauncher
Start-Class: com.dev.codeadda.banner.SpringBootCustomBannerApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Created-By: Apache Maven 3.3.9
Build-Jdk: 1.8.0_73
Implementation-URL: https://projects.spring.io/spring-boot/#/spring-boot-starter-parent/spring-boot-custom-banner

Now we have to create the jar file of our application, so that MANIFEST.MF will be created. Right click on “spring-boot-custom-banner” -> Run As -> Maven build (clean install). This will create the jar file in the target directory of project. MANIFEST.MF is created inside the jar archive in META-INF folder. Now run this executable jar as follows.

D:\Eclipse-Workspace\SpringBootTuts\spring-boot-custom-banner\target>java -jar spring-boot-custom-banner-0.0.1-SNAPSHOT.jar

Generating banner  programmatically

Yes, you can also change banner programmatically. For this spring boot provides org.springframework.boot.Banner interface. Override printBanner() method and provide your own implementation. Let us create “CustomBanner.java” inside “com.dev.codeadda.banner” package.

Now you have to register the above banner class “CustomBanner” with SpringApplication. For this use the SpringApplication.setBanner(…​) method as below.

If you run the above class, following custom banner will be displayed.

Note: The above banner defined by printBanner() method will only be displayed if we are not using banner.txt file in our classpath and also we have not configured spring.banner.location property in application.properties file.

Turn – Off the banner

You can even choose to disable the banner. This can be done in two different ways.

  1. Using the following property in application.properties file.
spring.main.banner-mode = off

2. Programmatically using SpringApplication.setBannerMode(..) method.

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

Different other modes are available such as Banner.Mode.CONSOLE to print the banner on console, Banner.Mode.LOG to print on log etc.

Final Words

In this article we saw different ways to use our own custom banners in place of default banners. I hope you have enjoyed this. If you loved my work, kindly subscribe our blog and in case of any problem let us know in the comment section below. Happy Koding…Keep Learning.

Be the first to comment

Leave a Reply