Fork me on GitHub

Spring Boot Banner自定义

目录

TIPS

本文基于Spring Boot 2.1.4,理论支持Spring Boot所有版本。

相信玩过Spring Boot的童鞋一定在启动日志中见过类似如下的内容。本文详细探讨如何定制这部分内容,让内容更加趣味性。

1
2
3
4
5
6
7
  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.4.RELEASE)

如何自定义

自定义Banner非常简单,只需在 classpathsrc/main/resources )下创建创建名为 banner.txt 的文件即可。

Banner生成工具

自己画Banner是很麻烦的,下面提供几款工具,将图片转换成ASCII字符,快速生成Banner。

工具地址 作用
http://patorjk.com/software/taag 写文字,选择字体,将字转成ASCII
http://picascii.com/ 上传图片,将图片转ASCII
其他 使用搜索引擎,搜索”图片转ASCII”即可

下面是笔者准备好的Banner:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
                    _ooOoo_
o8888888o
88" . "88
(| ^_^ |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . ___
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
========`-.____`-.___\_____/___.-`____.-'========
\ \ `-. \_ __\ /__ _/ .-` / /
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 永不宕机 永无Bug

占位符与描述信息

banner.txt 支持占位符,占位符可用于描述项目,同时也可定制Banner显示的具体细节。

允许使用的占位符如下表所示:

Variable Description
${application.version} 应用版本,从MANIFEST.MF 读取Implementation-Version 的值并显示。例如Implementation-Version: 1.0 ,则打印 1.0
${application.formatted-version} 将应用版本用括号括起来,并添加前缀v。例如:Implementation-Version: 1.0 ,则打印 (v1.0)
${spring-boot.version} 打印Spring Boot版本,例如 2.1.4.RELEASE
${spring-boot.formatted-version} 将Spring Boot版本用括号括起来,并添加前缀v。例如: (v2.1.4.RELEASE)
${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME}) 指定ANSI转义码,详见 org.springframework.boot.ansi.AnsiPropertySource
${application.title} 应用标题,从 MANIFEST.MF 读取 Implementation-Title 的值并打印。例如 Implementation-Title: itmuch-app ,则打印 itmuch-app

测试

创建 banner.txt ,内容如下:

1
2
3
4
${AnsiBackground.BRIGHT_YELLOW}${AnsiColor.BLUE}${AnsiStyle.BOLD}
应用版本:${application.version}
Spring Boot版本:${spring-boot.version}
应用标题:${application.title}

构建好应用后,启动日志将会打印类似如下图的内容:

banner-1

图片支持

Spring Boot同样支持使用图片作为Banner,只需将图片放到项目的classpathsrc/main/resources )目录下,命名为banner ,格式支持pngjpggif

不妨测试一下——

banner-2

由图可知,Spring Boot会自动将图片转换成ASCII字符展示。此外,还可在 application.yml 中使用 spring.banner.image.* 配置图片Banner显示的具体细节。

TIPS

看到这里,聪明的你一定会想到:不借助本文介绍的 Banner生成工具 也可生成文字Banner!只需将图片命名为banner.jpg/png/gif,然后将打印出来的Banner日志复制出来,并命名为 banner.txt 即可。

禁用Banner

添加如下配置:

1
2
3
spring:
main:
banner-mode: "off"

或在启动类上添加类似如下代码:

1
2
3
4
5
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}

福利

最后献上笔者项目中所使用的Banner。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
${AnsiColor.GREEN}
_ooOoo_
o8888888o
88" . "88
(| ^_^ |)
O\ = /O
____/`---'\____
.' \\| |// `.
/ \\||| : |||// \
/ _||||| -:- |||||- \
| | \\\ - /// | |
| \_| ''\---/'' | |
\ .-\__ `-` ___/-. /
___`. .' /--.--\ `. . ___
."" '< `.___\_<|>_/___.' >'"".
| | : `- \`.;`\ _ /`;.`/ - ` : | |
========`-.____`-.___\_____/___.-`____.-'========
\ \ `-. \_ __\ /__ _/ .-` / /
`=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
佛祖保佑 永不宕机 永无Bug
==================================================
Application Info:${application.title}-${application.version}
Powered by:Spring Boot ${spring-boot.version}

效果:

banner-3

灵感启迪

如果你对Banner放什么感到茫然,笔者给你几个思路:

  • 项目愿景/期望(例如上面,笔者就期望永不宕机、永无Bug)
  • 项目/团队/企业Slogan(好的项目都有一个好的Slogan)
  • 你们老板的帅照(会跪舔很重要啊)
  • 各种恶搞

相关源码

  • org.springframework.boot.SpringApplicationBannerPrinter
  • org.springframework.boot.SpringBootBanner

比较简单,对原理感兴趣的童鞋可以研究一下。

配套代码

相关文章

评论系统未开启,无法评论!