返回正文
SpringBoot 控制台秒变炫彩特效
一、自定义 启动横幅 banner.txt
什么是 banner.txt?
当你启动 Spring Boot 应用时,控制台会打印出一个默认的 Spring Boot Banner(比如启动 LOGO)。这个内容来自 resources 目录下的 banner.txt 文件。你可以通过这个文件自定义你想展示的 ASCII 图案、版本号、提示文字等。
创建或修改 banner.txt
文件位置
在你的 Spring Boot 项目中,进入 src/main/resources 目录下,新建或编辑:
txt
src/main/resources/banner.txt1
内容示例(纯色风格 + 启动信息)
bash
===========================================
== AbsWeb 启动成功! 🚀 ==
== 端口:${server.port} ==
== 环境:${spring.profiles.active} ==
== 时间:${date} ==
===========================================1
2
3
4
5
6
2
3
4
5
6
你可以使用 Spring Boot 提供的一些占位符变量:
| 占位符 | 描述 |
|---|---|
${application.version} | 项目的 MANIFEST.MF 版本 |
${application.formatted-version} | 格式化版本号 |
${spring-boot.version} | Spring Boot 版本 |
${server.port} | 当前端口号 |
${spring.profiles.active} | 当前激活的配置环境 |
${date} | 启动时间 |
${AnsiColor.NAME} | 控制台颜色(如 GREEN、RED) |
加点颜色和风格(可选)
Spring Boot 支持 ANSI 颜色,在支持的终端中会显示彩色效果:
txt
${AnsiColor.BRIGHT_CYAN}
===========================================
== TGS-XIYUE 启动成功! 🚀 ==
== 端口:${server.port} ==
== 环境:${spring.profiles.active} ==
== 时间:${date} ==
===========================================
${AnsiColor.DEFAULT}1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
启动测试
启动项目时,你会看到类似输出:
txt
===========================================
== TGS-XIYUE 启动成功! 🚀 ==
== 端口:8082 ==
== 环境:test ==
== 时间:2025-06-13 16:52:10 ==
===========================================1
2
3
4
5
6
2
3
4
5
6
提供几个网站可以自己去生成图案:
二、进阶方式:关闭默认 Banner,自定义 启动内容
禁用 Spring Boot 的默认 banner
在你的配置文件(如 application.yml 或 application.properties)中添加:
yml 文件:
yaml
spring:
main:
banner-mode: "off"1
2
3
2
3
properties 文件:
ini
spring.main.banner-mode=off1
自定义启动打印代码
在主启动类中添加一个打印方法,利用 ANSI 转义码输出彩色字符:
java
public class TgsXiyueApplication {
public static void main(String[] args) {
SpringApplication app = new SpringApplication(TgsXiyueApplication.class);
// 获取当前激活的Profile
Environment env = app.run(args).getEnvironment();
String activeProfiles = String.join(",", env.getActiveProfiles());
System.out.println("应用启动成功!");
System.out.println("激活的Profile: " + (activeProfiles.isEmpty() ? "无" : activeProfiles));
System.out.println("服务地址: http://localhost:" + env.getProperty("server.port") +
env.getProperty("server.servlet.context-path", ""));
}
private static boolean bannerPrinted = false; // 控制只打印一次
/**
* 在 ApplicationStartedEvent 事件中打印彩色 Banner
* 这个事件在 Spring 容器启动后、ApplicationReadyEvent 之前触发,
* 能够保证只执行一次(因为监听器是单例的)。
*/
@EventListener(ApplicationStartedEvent.class)
public void printColorfulBannerOnStartup() {
// 双重检查锁,防止多线程环境下重复打印
if (!bannerPrinted) {
synchronized (TgsXiyueApplication.class) {
if (!bannerPrinted) {
printColorfulBanner();
bannerPrinted = true;
}
}
}
}
/**
* 打印彩色的 ASCII 艺术字 Banner
* 利用 ANSI 转义码设置颜色,使用 JDK 文本块格式化字符串
*/
private static void printColorfulBanner() {
// ANSI 颜色代码
final String RESET = "\u001B[0m";
final String RED = "\u001B[31m";
final String GREEN = "\u001B[32m";
final String YELLOW = "\u001B[33m";
final String BLUE = "\u001B[34m";
final String PURPLE = "\u001B[35m";
final String CYAN = "\u001B[36m";
// 使用 JDK 文本块(JDK 15+)定义 ASCII 艺术字
String asciiArt = """
___________________ _________ ____ ___.________.___.____ ______________
\\__ ___/ _____/ / _____/ \\ \\/ /| \\__ | | | \\_ _____/
| | / \\ ___ \\_____ \\ ______ \\ / | |/ | | | /| __)_
| | \\ \\_\\ \\/ \\ /_____/ / \\ | |\\____ | | / | \\
|____| \\______ /_______ / /___/\\ \\|___|/ ______|______/ /_______ /
\\/ \\/ \\_/ \\/ \\/
""";
// 打印彩色 Banner - 为每一行设置不同的渐变色
String[] lines = asciiArt.split("\n");
// 定义颜色数组,实现渐变效果
String[] colors = {RED, YELLOW, GREEN, CYAN, BLUE, PURPLE};
System.out.println(); // 空行
for (int i = 0; i < lines.length; i++) {
if (i < lines.length && i < colors.length) {
// 为每一行应用不同的颜色
System.out.println(colors[i % colors.length] + lines[i] + RESET);
} else {
// 剩余行用青色
System.out.println(CYAN + lines[i] + RESET);
}
}
// 打印彩色分隔线和 JDK 版本信息
System.out.println(PURPLE + "========================================" + RESET);
// 获取 JDK 版本 - 这是 JDK 9 引入的新特性
Runtime.Version version = Runtime.version();
System.out.println(GREEN + "JDK Version: " + version + RESET);
System.out.println(BLUE + "Spring Boot with JDK 25" + RESET);
System.out.println(PURPLE + "========================================" + RESET);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
输出结果如下:
text
___________________ _________ ____ ___.________.___.____ ______________
\__ ___/ _____/ / _____/ \ \/ /| \__ | | | \_ _____/
| | / \ ___ \_____ \ ______ \ / | |/ | | | /| __)_
| | \ \_\ \/ \ /_____/ / \ | |\____ | | / | \
|____| \______ /_______ / /___/\ \|___|/ ______|______/ /_______ /
\/ \/ \_/ \/ \/
========================================
JDK Version: 25.0.2+10-LTS
Spring Boot with JDK 25
========================================1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
三、实现启动成功后的彩色提示
java
public class AbsWebApplication {
public static void main(String[] args) {
SpringApplication.run(AbsWebApplication.class, args);
printStartupMessage();
}
private static void printStartupMessage() {
String reset = "\u001B[0m";
String red = "\u001B[31m";
String yellow = "\u001B[33m";
String green = "\u001B[32m";
String cyan = "\u001B[36m";
String magenta = "\u001B[35m";
String blue = "\u001B[34m";
System.out.println(cyan + " (\\_/) " + reset);
System.out.println(cyan + " ( •_•) " + reset + red + " TgsXiyueApplication 启动成功!" + reset);
System.out.println(cyan + " / >🍪 " + reset + yellow + " 祝你今天工作顺利,项目无BUG~" + reset);
System.out.println();
System.out.println(magenta + " ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓" + reset);
System.out.println(magenta + " ┃ 🎉 欢迎使用 TGS-XIYUE!🎉 ┃" + reset);
System.out.println(magenta + " ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛" + reset);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
V 0.12.9 |
基于 MIT Licensed版权所有 © 2009- 2026 CMONO.NET
本站访客数
--次 本站总访问量
--人次 