Custom banner in spring boot

Upasana | October 29, 2019 | 4 min read | 9,257 views


When we start any spring boot app, it comes up with a default ascii banner on the console. In this tutorial we will learn how to create a custom banner for app startup.

Spring boot accepts ascii text, gif, jpg and png files for the custom startup banners.

Configuring Ascii text banner is really a two step process:

  1. Generate Ascii text banner using online tool

  2. Save it in src/main/resources/banner.txt file

Part 1. Creating custom ascii banner

We will use an online Spring Boot Ascii Banner generator to create custom banner which will be displayed during the application startup. There are multiple sites that allow us to do this:

You can use any of the above to generate text of your choice.

Part 2. Configuring Ascii banner in Spring Boot

Now we have created Ascii text banner, we need to save this text file with name banner.txt under src/main/resources so that Spring Boot can pick it up and display it at application startup.

Spring Boot by default will pick content from the banner.txt file under resources folder and display it on the startup.

If you want, you can customize this behaviour by specifying custom location for banner text, which can be configured in application.properties file using banner.location property.

application.properties
spring.banner.location=classpath:custom-banner.txt

or if you are using yaml format:

application.yml
spring.banner.location: classpath:custom-banner.txt
Here is the sample program output
,--.   ,--.         ,--.
|  |   |  |  ,---.  |  |  ,---.  ,---.  ,--,--,--.  ,---.
|  |.'.|  | | .-. : |  | | .--' | .-. | |        | | .-. :
|   ,'.   | \   --. |  | \ `--. ' '-' ' |  |  |  | \   --.
'--'   '--'  `----' `--'  `---'  `---'  `--`--`--'  `----'


2019-05-24 00:03:12.334  INFO 7191 --- [           main] com.shunya.comments.ShunyaApp            : The following profiles are active: dev,default
2019-05-24 00:03:13.185  INFO 7191 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.

Specifying font and background color

We can also specify font and background color of the ascii banners using the below format:

For example, to choose white background and red font color, your ascii text banner will look like this:

banner.txt
${AnsiBackground.WHITE}${AnsiColor.RED}
,--.   ,--.         ,--.
|  |   |  |  ,---.  |  |  ,---.  ,---.  ,--,--,--.  ,---.
|  |.'.|  | | .-. : |  | | .--' | .-. | |        | | .-. :
|   ,'.   | \   --. |  | \ `--. ' '-' ' |  |  |  | \   --.
'--'   '--'  `----' `--'  `---'  `---'  `--`--`--'  `----'

Using banner variables

There is a provision for specifying additional banner variables inside your banner.txt file. You can use any of the following placeholders:

Variable Description

${application.title}

The title of your application, as declared in MANIFEST.MF

${spring-boot.version}

The Spring Boot version i.e. 2.2.0

${spring-boot.formatted-version}

Formatted Spring Boot version i.e. (v2.2.0.BUILD-SNAPSHOT)

${application.version}

The version number of your application, as declared in MANIFEST.MF, example 1.0

${application.formatted-version}

The formatted version number of your application, as declared in MANIFEST.MF, example (v1.0)

Example ascii banner.txt with these placeholders will look like the following:

banner.txt
${AnsiBackground.WHITE}${AnsiColor.RED}
,--.   ,--.         ,--.
|  |   |  |  ,---.  |  |  ,---.  ,---.  ,--,--,--.  ,---.
|  |.'.|  | | .-. : |  | | .--' | .-. | |        | | .-. :
|   ,'.   | \   --. |  | \ `--. ' '-' ' |  |  |  | \   --.
'--'   '--'  `----' `--'  `---'  `---'  `--`--`--'  `----'
Spring Boot ${spring-boot.version}
${application.title} - ${application.formatted-version}${AnsiBackground.DEFAULT}

We can always put ${AnsiBackground.DEFAULT} at the end of the file to restore default background color on the console.

Part 3. Image banners

In addition to a text file, you can also add a banner.gif, banner.jpg, or banner.png image file to your classpath or set the spring.banner.image.location property. Images are converted into an ASCII art representation and printed above any text banner. Other option is to upload these images to online tool and convert them to ASCII text.

/src/main/resources/application.yml
spring.banner.image.location: classpath:banner.gif

Part 4. Additional configuration

Banner mode: Console, Log and off

We can configure if the spring boot banner will be printed to System.out (console), print to log file (log), or disable printing at all (off)

Banner mode configuration values
banner-mode Description

console

print banner to console (System.out)

log

print banner to log file

off

disable banner printing

For example, the following configuration will print the banner to console.

application.yml
spring.main.banner-mode: console

Optionally, you can also configure this behavior using Java code:

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

Generate banner programmatically

You can use SpringApplication.setBanner(…​) method to programmatically generate a banner in Spring Boot application. All you need to do is to provide your own implementation for org.springframework.boot.Banner interface.

An example implementation will look like this:

MyApplication.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(MyApplication.class);
        app.setBanner((environment, sourceClass, out) -> {
            out.println("Javacodemonk Welcome Banner");
        });
        app.run(args);
    }
}

That’s all for now. For more information, you can refer to Spring Boot documentation:


Top articles in this category:
  1. Setting a Random Port in Spring Boot Application at startup
  2. Custom TTL for Spring data Redis Cache
  3. Redis rate limiter in Spring Boot
  4. Basic Auth Security in Spring Boot 2
  5. SendGrid emails in Spring Boot
  6. Testing web layer in Spring Boot using WebMvcTest
  7. Feign RequestInterceptor in Spring Boot

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.