目標
實現自定義啓動畫面
操作步驟
添加依賴
引入 Spring Boot Starter 父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
引入 spring-boot-starter-web
依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
自定義 Banner
spring-boot 支持多種方式設置 banner,也可以不輸出 banner
字符串形式
在 src/main/resources
路徑下新建一個 banner.txt 文件,在 banner.txt 中填寫需要打印的字符串內容即可。
方法很簡單,但首先得繪製一個有意思的字符串,比如如下的 HelloWorld:
.__ .__ .__ .__ .___
| |__ ____ | | | | ____ __ _ _____________| | __| _/
| | \_/ __ \| | | | / _ \ \ \/ \/ / _ \_ __ \ | / __ |
| Y \ ___/| |_| |_( <_> ) \ ( <_> ) | \/ |__/ /_/ |
|___| /\___ >____/____/\____/ \/\_/ \____/|__| |____/\____ |
\/ \/ \/
一般情況下,我們會藉助第三方工具幫忙轉化內容:
- 文字轉化成字符串:http://www.network-science.de/ascii/
- 圖片轉化成字符串:http://www.degraeve.com/img2txt.php
banner.txt配置
- ${AnsiColor.BRIGHT_RED}:設置控制檯中輸出內容的顏色
- ${application.version}:用來獲取MANIFEST.MF文件中的版本號
- ${application.formatted-version}:格式化後的${application.version}版本信息
- ${spring-boot.version}:Spring Boot的版本號
- ${spring-boot.formatted-version}:格式化後的${spring-boot.version}版本信息
圖片形式
將圖片重命名爲 banner.xxx
(xxx 爲圖片後綴名,比如 banner.gif),放在 src/main/resources
目錄下
源碼地址
本章源碼 : https://gitee.com/gongm_24/spring-boot-tutorial.git
結束語
更換 spring-boot 原本的 banner 爲個性化的輸出,可以輸出公司 Logo 或者項目 Logo 什麼的,增加參與成員對公司、項目、品牌的認同感