In this post, we’ll discuss the banner feature in Spring Boot applications. The banner is presented in the console window when the Spring Boot application begins. By default, the Spring Boot application showcases the text “Spring” as a banner in the console window. However, Spring Boot provides flexibility to customize the banner according to the specific needs of your application. Here’s a guide on creating a personalized Spring Boot banner.

Default Spring Boot Banner

As mentioned earlier, the Spring Boot application is inherently set up to exhibit a banner upon initialization. When you execute the Spring Boot application, it will showcase the term “Spring” as a banner, incorporating special characters for visual appeal. The log excerpt from the console window below illustrates the appearance of the Spring Boot banner at the commencement of the application.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.7.4)

2023-04-14 17:10:00.405  INFO 9744 --- [           main] com.test.SpringBootBannerApplication    : Starting SpringBootBannerApplication using Java 17.0.4 on DESKTOP-DMI8H0A with PID 9744 (E:\STS\workspace\SpringBootBanner\target\classes started by Admin in E:\STS\workspace\SpringBootBanner)
2023-04-14 17:10:00.410  INFO 9744 --- [           main] com.test.SpringBootBannerApplication    : No active profile set, falling back to 1 default profile: "default"
2023-04-14 17:10:01.174  INFO 9744 --- [           main] com.test.SpringBootBannerApplication    : Started SpringBootBannerApplication in 1.242 seconds (JVM running for 2.221)

How to Disable Spring Boot Banner

Customizing the Spring Boot banner within your application is entirely optional. Spring Boot provides seamless support for disabling the application’s banner. Easily conceal the Spring Boot banner by configuring the application properties file. Alternatively, employ Java code and command line arguments for an alternative configuration mode. Within the application properties file, utilize the “spring.main.banner-mode” property to effortlessly toggle the banner’s visibility, granting you versatile control over its display status.

application.properties

spring.main.banner-mode=off

Deactivate the banner in your Spring Boot application by utilizing Java code. Embed the pertinent banner disable code into the main class using the SpringApplicationBuilder class. This enables seamless customization, ensuring the banner aligns with your application’s visual preferences.

SpringBootBannerApplication.java

package com.test;

import org.springframework.boot.Banner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class SpringBootBannerApplication {

	public static void main(String[] args) {
		//SpringApplication.run(SpringBootBannerApplication.class, args);
		SpringApplicationBuilder builder = new SpringApplicationBuilder(SpringBootBannerApplication.class);
        builder.bannerMode(Banner.Mode.OFF);
        builder.run(args);
	}

}

You can utilize a command line argument to deactivate the Spring Boot banner. The provided command illustration demonstrates how to turn off the Spring Boot banner within the application.

Java Command Line Argument

Project\target>java -jar -Dspring.main.banner-mode=off SpringBootBanner-0.0.1-SNAPSHOT.jar

Spring Boot Command Line Argument

Project>mvn spring-boot:run -Dspring.main.banner-mode=off

How to Change Default Banner Text

Spring Boot facilitates the customization of the default banner text within your application. By creating a text file named banner.txt in the src/main/resources folder, you can seamlessly alter the displayed text. Upon launching the Spring Boot application, the specified text from the banner.txt file will be presented at the application’s initiation. Various online platforms provide Spring Boot banner generators, enabling the creation of visually appealing banners for your text. It is essential to generate a banner using these tools and integrate it into the banner.txt file to ensure its display at the commencement of your application.

src/main/resources/banner.txt

Welcome to Hello Word

Output

Welcome to Hello Word

2023-04-14 17:18:44.284  INFO 9824 --- [           main] com.test.SpringBootBannerApplication    : Starting SpringBootBannerApplication using Java 17.0.4 on DESKTOP-DMI8H0A with PID 9824 (E:\STS\workspace\SpringBootBanner\target\classes started by Admin in E:\STS\workspace\SpringBootBanner)
2023-04-14 17:18:44.290  INFO 9824 --- [           main] com.test.SpringBootBannerApplication    : No active profile set, falling back to 1 default profile: "default"
2023-04-14 17:18:44.992  INFO 9824 --- [           main] com.test.SpringBootBannerApplication    : Started SpringBootBannerApplication in 1.271 seconds (JVM running for 2.282)

How to use a different location or file to replace the default banner

You can substitute the default banner with a file from an alternate location or with a distinct filename. Configuring the banner file can be achieved through two methods: utilizing the Java classpath or specifying the file path. The Spring Boot application properties file accommodates settings for both configurations. The ensuing example will illustrate the steps to appropriately configure the banner file.

src/main/resources/test.txt

Welcome to Hello Word

Using ClassPath, application.properties

spring.banner.location=classpath:test.txt

Using FilePath, application.properties

spring.banner.location=file:src/main/resources/test.txt

Output

Welcome to Hello Word

2023-04-14 17:18:44.284  INFO 9824 --- [           main] com.test.SpringBootBannerApplication    : Starting SpringBootBannerApplication using Java 17.0.4 on DESKTOP-DMI8H0A with PID 9824 (E:\STS\workspace\SpringBootBanner\target\classes started by Admin in E:\STS\workspace\SpringBootBanner)
2023-04-14 17:18:44.290  INFO 9824 --- [           main] com.test.SpringBootBannerApplication    : No active profile set, falling back to 1 default profile: "default"
2023-04-14 17:18:44.992  INFO 9824 --- [           main] com.test.SpringBootBannerApplication    : Started SpringBootBannerApplication in 1.271 seconds (JVM running for 2.282)

How to change Banner Color

Modify the banner’s color by leveraging predefined variables within the banner.txt file. The variable ${AnsiColor.COLOR NAME} dictates the banner’s color, with the specific color name replacing the variable. In the given example, the banner color will be set to blue. Revert to the default color by employing the “default” keyword within the variable when needed.

banner.txt

${AnsiColor.BLUE}
 \ \      / /__| | ___ ___  _ __ ___   ___ 
  \ \ /\ / / _ \ |/ __/ _ \| '_ ` _ \ / _ \
   \ V  V /  __/ | (_| (_) | | | | | |  __/
    \_/\_/ \___|_|\___\___/|_| |_| |_|\___|
${AnsiColor.DEFAULT}

Output

 \ \      / /__| | ___ ___  _ __ ___   ___ 
  \ \ /\ / / _ \ |/ __/ _ \| '_ ` _ \ / _ \
   \ V  V /  __/ | (_| (_) | | | | | |  __/
    \_/\_/ \___|_|\___\___/|_| |_| |_|\___|
                                           
2023-04-14 17:18:00.907  INFO 1104 --- [           main] com.test.SpringBootBannerApplication    : Starting SpringBootBannerApplication using Java 17.0.4 on DESKTOP-DMI8H0A with PID 1104 (E:\STS\workspace\SpringBootBanner\target\classes started by Admin in E:\STS\workspace\SpringBootBanner)
2023-04-14 17:18:00.910  INFO 1104 --- [           main] com.test.SpringBootBannerApplication    : No active profile set, falling back to 1 default profile: "default"
2023-04-14 17:18:01.526  INFO 1104 --- [           main] com.test.SpringBootBannerApplication    : Started SpringBootBannerApplication in 1.098 seconds (JVM running for 1.983)

Leave a Reply