首页 > 编程语言 > 详细

Spring Boot自定义Banner

时间:2020-05-17 16:42:40      阅读:47      评论:0      收藏:0      [点我收藏+]

Spring Boot项目启动的时候会打印如下内容。

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

 

我们要怎么自定义打印的内容呢?

自定义Banner非常简单,只需在 classpath ( src/main/resources )下创建创建名为 banner.txt 的文件,然后在banner.txt写入自己想要打印的内容即可。

比如:

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

 

如何打印项目相关的信息?

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

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

VariableDescription
${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 。

比如:

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

 

怎么禁用Banner?

添加如下配置:

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

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

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

 

结束语:重要的不是你从哪里来,而是你到哪里去,找准方向,继续努力。

 

佛系博主:AlanLee

博客地址:http://www.cnblogs.com/AlanLee

GitHub地址:https://github.com/AlanLee-Java

本文出自博客园,欢迎大家加入博客园。

 

Spring Boot自定义Banner

原文:https://www.cnblogs.com/AlanLee/p/12905700.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!