如何用Spring Boot自定义Banner - AlanLee,案例详解

Spring Boot项目启动的时候会打印如下内容。
1 . ____ _ __ _ _ 2 /\ / ___"_ __ _ _(_)_ __ __ _ 3 ( ( )\___ | "_ | "_| | "_ / _` | 4 \/ ___)| |_)| | | | | || (_| | ) ) ) ) 5 " |____| .__|_| |_|_| |_\__, | / / / / 6 =========|_|==============|___/=/_/_/_/ 7 :: Spring Boot :: (v2.1.4.RELEASE)
注意:光理论是不够的。在此顺便送大家十套2020最新JAVA架构项目实战教程及大厂面试题库,进我扣裙 :七吧伞吧零而衣零伞 (数字的谐音)转换下可以找到了,还可以跟老架构师交流
我们要怎么自定义打印的内容呢?
自定义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显示的具体细节。
允许使用的占位符如下表所示:
| 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。 | 
比如:
 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 }
附上我使用的Banner:
======Powered by:Spring Boot ${spring-boot.version}======
                    _ooOoo_
                   o8888888o
                   88" . "88
                   (| ^_^ |)
                   O  =  /O
                ____/`---"\____
              ."  \|     |//  `.
             /  \|||  :  |||//  
            /  _||||| -:- |||||-  
            |   | \  -  /// |   |
            | \_|  ""---/""  |   |
              .-\__  `-`  ___/-. /
          ___`. ."  /--.--  `. . ___
        ."" "<  `.___\_<|>_/___."  >""".
      | | :  `- `.;` _ /`;.`/ - ` : | |
========`-.____`-.___\_____/___.-`____.-"========
         `-.   \_ __ /__ _/   .-` /  /
                     `=---="
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      佛祖保佑       永不宕机       永无Bug
都懂了吗?最后注意:顺便送大家十套2020最新JAVA架构项目实战教程及大厂面试题库,进我扣裙 :七吧伞吧零而衣零伞 (数字的谐音)转换下可以找到了,还可以跟老架构师交流
本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理
 
                    
                
 
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号