Customizing the Spring Boot Console Banner: Creation, Styling, Property Retrieval, and Disabling
This guide explains how to customize the Spring Boot startup banner by creating a banner.txt file, using ANSI color codes, retrieving various Spring Boot properties, and optionally disabling the banner altogether through code changes.
When a Spring Boot project starts, a default banner is displayed in the console, which may look unfamiliar.
You can customize this banner by creating a banner.txt file under the resources directory.
// banner.txt
JimmyAfter restarting the application, the new banner appears.
Retrieving Properties
You can access various Spring Boot properties using the ${propertyName} syntax, for example:
${spring-boot.version}Common properties include:
AnsiColor.BRIGHT_RED : sets the console text color (see org.springframework.boot.ansi.AnsiColor ).
application.version : version information from the MANIFEST.MF file.
application.formatted-version : formatted version derived from application.version .
spring-boot.version : the Spring Boot version number.
spring-boot.formatted-version : formatted Spring Boot version.
Setting Colors
To color console output, prepend the desired AnsiColor enum value, such as ${AnsiColor.BRIGHT_RED} , to the text.
// Enum values
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
package org.springframework.boot.ansi;
public enum AnsiColor implements AnsiElement {
DEFAULT("39"),
BLACK("30"),
RED("31"),
GREEN("32"),
YELLOW("33"),
BLUE("34"),
MAGENTA("35"),
CYAN("36"),
WHITE("37"),
BRIGHT_BLACK("90"),
BRIGHT_RED("91"),
BRIGHT_GREEN("92"),
BRIGHT_YELLOW("93"),
BRIGHT_BLUE("94"),
BRIGHT_MAGENTA("95"),
BRIGHT_CYAN("96"),
BRIGHT_WHITE("97");
private final String code;
private AnsiColor(String code) {
this.code = code;
}
public String toString() {
return this.code;
}
}Practical Example
Generate ASCII art using an online tool and place it in banner.txt along with color codes:
启动成功!
${AnsiColor.BRIGHT_RED}
_ _
| (_)
| |_ _ __ ___ _ __ ___ _ _
_ | | | '_ ` _ \ / _` \ \ / / | |
| |__| | | | | | | | (_) \ V /| |_| |
____/|_|_|_| |_| |_|\___/ \_/ \__, |
__/ |
|___/
${AnsiColor.BRIGHT_WHITE}
欢迎使用~
spring boot 版本为 ${spring-boot.version}After restarting, the console displays the customized banner.
Disabling the Banner
If you prefer not to show any banner, modify the main application class to turn off the banner mode:
import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LaunchApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(LaunchApplication.class);
app.setBannerMode(Banner.Mode.OFF); // Disable banner
app.run(args);
}
}Running the application now produces no banner output.
Source: juejin.cn/post/7259965990960201787
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.