🏠 ❯ Spring Boot ❯ Spring Boot Banner – Complete Guide

Spring Boot Banner – Complete Guide

In this post, We will learn how to customize startup banner of a spring Boot application. Also, we will understand how we can use an image or gif file as a startup banner.

Take a look at our Hello World Application. As you see, the default at the spring boot startup banner looks like below.

Default Spring Boot Banner

In this output, first few lines show the text Spring in ASCII format followed by the spring boot version. Let’s See how to customize it.

Adding custom Spring Boot banner text

To provide a custom spring boot banner you need to create a banner.txt under src/main/resources. If you want to learn how the resources directory works, then read more about Spring Boot 2: Directory structure and Convention. Let’s test this by creating a banner.txt. You can use a service like Online Spring Boot Banner Generator.

  ____             _             _   _               
 / ___| _ __  _ __(_)_ __   __ _| | | | _____      __
 \___ \| '_ \| '__| | '_ \ / _` | |_| |/ _ \ \ /\ / /
  ___) | |_) | |  | | | | | (_| |  _  | (_) \ V  V / 
 |____/| .__/|_|  |_|_| |_|\__, |_| |_|\___/ \_/\_/  
       |_|                 |___/                     Code language: plaintext (plaintext)

Once the above file is added to the project, the spring boot startup banner changes as shown below.

Custom banner text

Banners with Spring Boot version

You can add context to the banner by adding predefined placeholders. For example, Adding the placeholders ${application.title}${application.formatted-version} and ${spring-boot.formatted-version} placeholders will pull information from the application JAR’s MANIFEST.MF file and display it on the banner section. Here is a sample output for that.

Banner with placeholders

Adding colours To the banner text

One more thing we can do is to add colours to our banners. This feature is possible due to ANSI encoding support in spring boot. To enable colours you need to follow two steps.

Make sure the system supports ASCII colours.

For instance, Windows Command-Line and Powershell don’t support asci with colours. So don’t bother wasting time with these environments. But IDEs like IntelliJ, Eclipse and others usually support ANSI colours regardless of the OS. For example, All the screenshots you see here are from Intellij running on my Windows PC. But most of the Linux shells would be able to show colours on a TTY session. Spring Boot has internal logic to detect if a system supports coloured output or not. So you don’t have to do anything in this regard. If you want to keep the colours as default behaviour you need to set spring.output.ansi.enabled property to always. The default value for this property is detect.

spring.output.ansi.enabled=always

Make sure you markup the colours

To mark a text with yellow, wrap the content between ${AnsiColor.YELLOW} and ${AnsiColor.DEFAULT}. For background and text styles, the concept is the same but you should use AnsiBackground and AnsiStyle placeholder. Checkout the github Link at the end for the source code.

With all the above completed, You can see the coloured banner at the startup.

Banner text with colour

Images as Banners

So far, we saw the banner.txt and how to customize it. But the problem with this approach is that it becomes complex to design a logo out of it. In this case, You can use a banner.png or banner.jpg.

When you provide a banner image, Spring Boot will convert the image into ASCII text and use it at the startup. For example, I added the following image in src/main/resources.

And here is the output that was automatically generated by the spring boot.

using image as spring boot startup banner

Animated Spring Boot Banner using GIF

The surprise doesn’t end there. You can keep a GIF as a banner as well. Yes, An animated gif. For this, the banner file will be banner.gif. I have a simple banner.gif as shown below.

sample image to generate spring boot animated banner

And here is the result of adding gif file as a banner.

spring boot gif banner
an animated banner

At this point, I have to say that GIF based approach doesn’t fit for all as a few terminals support ASCII animation. But it is still fun and interesting to know that feature like these are buried in Spring Boot.

Using Image banners along with text banners

When using images as banners, you don’t have a way to provide application version information. For this reason, Spring boot will also load a banner.txt if an image was loaded. For example, the following screenshot shows that the startup banner contains both logo and text information.

spring boot custom banner with image and text

Changing Banner file location

The banner text and banner image files are by default under classpath:banner*. But if you want to move these files somewhere else but still want to print banners, you can use the spring.banner.location and spring.banner.image.location properties to set the appropriate file names/

spring.banner.location=banner-production.txt
spring.banner.image.location=banner-image.png

Interesting thing about this approach is that you can use different files in different environment to distinguish between production and test environments.

Turn off Spring boot Banners

You can disable the default banner by setting the spring.main.banner-mode property to off. The default value for this properties entry is console which means the output is not part of the logs. Apart from off and console this property can take log as one more value where the banner info is printed on the logs. When log is set, there is no guarantee that the colours would work.

spring.main.banner-mode=off

Programmatic Approach to Banners

We can also change the banner mode using the java configuration. For example, the following code will print banner directly to log entries instead of console. And the setBanner method helps us to provide a fallback Banner if a valid banner file is not present in the classpath.

        SpringApplication springApplication = new SpringApplication(SpringBootBannerExampleApplication.class);
        springApplication.setBannerMode(Banner.Mode.LOG);
        springApplication.setBanner((environment, sourceClass, out) -> out.println("A Fallback Banner...!"));
        springApplication.run(args);Code language: JavaScript (javascript)

Conclusion

We learned how we can change the startup banner. Even we learned to use an image or gif as a spring boot startup banner. If you enjoyed this title, you may also like the following write-ups.

Here is the github repository for the above examples.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

One Comment

  1. Dear friend,

    Thank you for your interesting and helpful application

    Please could you manage number and make T letter with left bar more visible ?

    Your heart and mind breaker.