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

痛定思痛。 2023-03-14 13:30 4阅读 0赞

Spring Boot自定义Banner - AlanLee

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

复制代码

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

复制代码

注意:光理论是不够的。在此顺便送大家十套2020最新JAVA架构项目实战教程及大厂面试题库,进我扣裙 :七吧伞吧零而衣零伞 (数字的谐音)转换下可以找到了,还可以跟老架构师交流

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

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

比如:

复制代码

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

复制代码

怎么禁用Banner?

添加如下配置:

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

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

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

附上我使用的Banner:

复制代码

  1. ======Powered bySpring Boot ${spring-boot.version}======
  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

都懂了吗?最后注意:顺便送大家十套2020最新JAVA架构项目实战教程及大厂面试题库,进我扣裙 :七吧伞吧零而衣零伞 (数字的谐音)转换下可以找到了,还可以跟老架构师交流

本文的文字及图片来源于网络加上自己的想法,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理

发表评论

表情:
评论列表 (有 0 条评论,4人围观)

还没有评论,来说两句吧...

相关阅读