Spring Boot banner is a text message that is displayed at the start of a Spring Boot application.


This is how the default banner looks like.

This banner can be turned off or it is also possible to create a custom banner.

In this article, we'll learn how to create a custom banner and use it in Spring Boot applications.

Table of Content :

Turning banner off

The banner can be disabled using configuration settings or programmatically.

We can turn off the banner with the s

spring.main.banner-mode property. spring.main.banner-mode=off 

 or 

 SPRING_MAIN_BANNER-MODE=off

It is also possible to disable the banner with the SPRING_MAIN_BANNER-MODE environment variable.

 
new SpringApplicationBuilder(MyApplication.class)
    .bannerMode(Banner.Mode.OFF)
    .run(args)

var app = new SpringApplication(Application.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args); 

Programatically, it is possible to turn off the banner with SpringApplicationBuilder or SpringApplication.


Spring Boot custom banner

We will use an online Spring Boot Ascii Banner generator to create a custom banner which will be displayed during the application startup.

You can use one of the following:

https://devops.datenkollektiv.de/banner.txt/index.html 

https://devops.datenkollektiv.de/banner.txt/index.html

http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20

 
     ,--.                               ,-----.            ,--.          ,---.     ,--.             ,---.  ,---.         
     |  |  ,--,--. ,--.  ,--.  ,--,--. '  .--./  ,---.   ,-|  |  ,---.  '   .-'  ,-'  '-. ,--.,--. /  .-' /  .-'  ,---.  
,--. |  | ' ,-.  |  \  `'  /  ' ,-.  | |  |     | .-. | ' .-. | | .-. : `.  `-.  '-.  .-' |  ||  | |  `-, |  `-, (  .-'  
|  '-'  / \ '-'  |   \    /   \ '-'  | '  '--'\ ' '-' ' \ `-' | \   --. .-'    |   |  |   '  ''  ' |  .-' |  .-' .-'  `) 
 `-----'   `--`--'    `--'     `--`--'  `-----'  `---'   `---'   `----' `-----'    `--'    `----'  `--'   `--'   `----'  
     
      

Configuring Ascii banner in Spring Boot

we need to create a file named banner.txt in the src/main/resources directory and paste the banner content into it.

if we want to choose any other location or another name for the banner, we need to set the spring.banner.location property in the application.properties file:

 
spring.banner.location=classpath:my-custom-banner.txt
spring.banner.image.location=classpath:my-custom-banner.png  

if we are using images as banner then following properties can be configured.

 
spring.banner.image.location=classpath:banner.gif
spring.banner.image.width= 150
spring.banner.image.height= 120
spring.banner.image.margin= 10
spring.banner.image.invert= value

It is recommended that to use text format because the application startup time will drastically increase if some complex image structure is used.

or if you are using YAML Configuration

application.yml spring.banner.location: classpath:my-custom-banner.txt
 
 
Here is the output  
     ,--.                               ,-----.            ,--.          ,---.     ,--.             ,---.  ,---.         
     |  |  ,--,--. ,--.  ,--.  ,--,--. '  .--./  ,---.   ,-|  |  ,---.  '   .-'  ,-'  '-. ,--.,--. /  .-' /  .-'  ,---.  
,--. |  | ' ,-.  |  \  `'  /  ' ,-.  | |  |     | .-. | ' .-. | | .-. : `.  `-.  '-.  .-' |  ||  | |  `-, |  `-, (  .-'  
|  '-'  / \ '-'  |   \    /   \ '-'  | '  '--'\ ' '-' ' \ `-' | \   --. .-'    |   |  |   '  ''  ' |  .-' |  .-' .-'  `) 
 `-----'   `--`--'    `--'     `--`--'  `-----'  `---'   `---'   `----' `-----'    `--'    `----'  `--'   `--'   `----'  
  


2020-05-31 00:11:14.159  INFO 6743 --- [           main] com.bala.spring.myapp            : The following profiles are active: dev,default
2020-05-31 00:11:15.175  INFO 6743 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data repositories in DEFAULT mode.
 

Using banner variables

We can customize the banner variables inside your banner.txt file. You can use following proerties

 
${application.title}

${spring-boot.version}

${spring-boot.formatted-version}

${application.version}

${application.formatted-version}


Generate banner programmatically

You can use SpringApplication.setBanner() method to programmatically generate a banner in the Spring Boot application.

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

@SpringBootApplication
public class MySpringApp {

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

 


That's it!! We have seen how to create Custom Banner in Spring Boot.