Custom Java Spring Boot Banner in Simple Steps

Posted by Marta on January 14, 2021 Viewed 253 times

Learn how to configure your spring boot application with your favorite banner and customize it further using images and including variables.

Card image cap

Hey there! Are you looking for a way to customize your spring boot application banner, so your application looks more personalized and professional?

This article will learn how to configure your spring boot application with your favorite banner and customize it further using images and including variables like the application version. Additionally, we will see how you can turn off the banner.

Usually, when you start up a spring boot application, the first thing to show is the spring banner followed by some logging lines.

Let’s dive in!

Create the Banner

You could start by creating the banner that you want to use. Maybe something related to your brand, your team, or anything you like. The banner is simply a text file containing some ASCII art. Fortunately, you don’t have to create the ASCII art yourself. Several websites will convert any text or image into ASCII art. See below some examples:

Once the ASCII art is generated, save it in a file using the .txt extension.

Configure Spring Boot Banner

Now that we have the banner, we need to configure our spring boot application to replace the original banner with our new one. You can achieve this by calling your banner file banner.txt and placing it within the src/main/resources folder.

Once you add the file, spring boot will replace the original banner with the content of the banner.txt file.

Configure the Banner Programmatically

It is also possible to customize your banner programmatically. You can achieve this by calling the method .setBanner() before start running the function. The .setBanner() requires you to provide a class that implements the interface Banner. This interface has only one method to complete, the .printBanner() method.

What does the .printBanner() method does? It will read any string you pass to it and print it out to the console or logging( See below line 22). You can replace this line with any text you like. In this example, I will read the content of the file myBanner.txt, located at the project root. Then pass the text to the PrintStream variable so it gets printed to the console. See all this in action below:

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;


@SpringBootApplication
public class App
{

    public static void main( String[] args )
    {
        SpringApplication app = new SpringApplication(App.class);
        app.setBanner(new Banner() {
            @Override
            public void printBanner(Environment environment, Class<?> sourceClass, PrintStream out) {
                String banner = readBanner(); // REPLACE WITH ANY STRING
                out.print(banner);
            }
        });
        app.run(args);

    }

    private static String readBanner(){
        try {
            String banner = new String(Files.readAllBytes(Paths.get("myBanner.txt")), StandardCharsets.UTF_8);
            return banner;

        }catch(IOException ex){
            return "Banner not found";
        }
    }
}

There is no need to do this since you can add the banner.txt in the resource folder, which is easier. However, I have included it because it illustrates how spring boot handles the banner internally.

Configure a Specific Location

We have seen that you need to add a file called banner.txt in the resource folder; however, Could the banner file has a different name? Or could it be in another folder? Yes, but in this case, you will need to indicate to spring boot where your banner is. You can use the property spring.banner.location, which tells to spring boot where to find the banner file.

You can configure this property, passing the path and banner file name. Add this property to the application.property file, located in the resources folder.

Spring Boot banner with Images

Another possibility when setting up a banner is passing an image instead of a text file. In this case, you wouldn’t need to generate the banner yourself. Spring Boot will grab the image and convert it into ASCII on the flight. All you need to do is setting up the following property:

spring.banner.image.location: facebook_logo.png

Remember to place the image in the resource folder. In this example, I used the following image:

hellocodeclub

And the output looked as follows:

Banner with version

What if you would like some dynamic information in the banner? For instance, your application version or the spring boot version used by the application. You can achieve this by adding placeholders to your banner. Here is the list of variables that you could add to the banner:

  • ${spring-boot.version}
  • ${spring-boot.formatted-version}
  • ${application.version}
  • ${application.formatted-version}
  • ${application.title}

More details about the variables here.

In case any of those variables are not configured, you could set its value in the application.properties file, or passing the value as a parameter in the command line. See this in action below:

Create the banner.txt file( located in the resources folder)

******************************************************
*****                                            *****
*****                 THE BANNER                 *****
*****                                            *****
******************************************************
     Spring Boot Version: ${spring-boot.version}
     Application Version: ${application.version}

Add application.properties file (located in the resources folder)

application.version: 1.0.0

And here is the output you will see when you start up your spring boot application:

Turn off the banner

Spring Boot also offers the option to turn off the banner. There are two ways to do this: setting the banner mode programmatically before starting the application or using a spring boot property. Let’s see both approaches.

#1. Turn off Banner Programmatically

The SpringApplication class offers a method to turn off the banner called .setBannerMode(), which takes the banner mode as a parameter. The banner mode available values are :

  • Banner.Mode.OFF
  • Banner.Mode.CONSOLE
  • Banner.Mode.LOG

Therefore to turn off the banner you should call the .setBannerMode() passing OFF as parameter. See example below, line 11:

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App2
{
    public static void main( String[] args )
    {
        SpringApplication app = new SpringApplication(App2.class);
        app.setBannerMode(Banner.Mode.OFF);
        app.run(args);     
    }
}

Once you have done this change, the banner will no longer show up when the application starts up.

#2. Using Spring Boot property

Another approach to completely remove the banner is adding the following spring boot property in the application.properties file, located in the resources folder(src/main/resources):

spring.main.banner-mode=off

Conclusion

To summarise, we have seen how to customize the spring boot banner in your application both programmatically and via spring boot properties. Additionally, we have also covered how to turn off the banner using two different approaches: programmatically and with spring boot properties.

I hope you find the article useful. Thank you so much for reading and supporting this blog! Happy Coding!

More Interesting Articles

Project-Based Programming Introduction

Steady pace book with lots of worked examples. Starting with the basics, and moving to projects, data visualisation, and web applications

100% Recommended book for Java Beginners

Unique lay-out and teaching programming style helping new concepts stick in your memory

90 Specific Ways to Write Better Python

Great guide for those who want to improve their skills when writing python code. Easy to understand. Many practical examples

Grow Your Java skills as a developer

Perfect Boook for anyone who has an alright knowledge of Java and wants to take it to the next level.

Write Code as a Professional Developer

Excellent read for anyone who already know how to program and want to learn Best Practices

Every Developer should read this

Perfect book for anyone transitioning into the mid/mid-senior developer level

Great preparation for interviews

Great book and probably the best way to practice for interview. Some really good information on how to perform an interview. Code Example in Java