SpringBoot自定義Banner資訊
一、介紹
本文主要介紹使用springboot框架時,我們可以自定義我們專案的相關資訊,例如啟動圖、啟動時的版本號等,
二、自定義banner
我們在啟動SpringBoot專案時,控制臺會列印出SpirngBoot的圖形,包含SpringBoot的版本號,這個圖形叫做banner,如下圖:接下來我們一起學習一下怎么自定義啟動圖形,

三、自定義banner步驟
-
首先我們需要在專案的resource檔案夾下新建新檔案,檔案名命名為banner.txt,我們需要自定義的圖形就放在該檔案里面,
-
我們可以在該網站http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something%20上面生成相應字體的圖案,例如下圖:

- 復制網站生成的相關字體資訊到banner.txt檔案中,
- 修改SpringApplication啟動類,在SpringApplication啟動類中啟動時修改為如下代碼:
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(StarBootApplication8080.class);
springApplication.setBannerMode(Banner.Mode.CONSOLE);
springApplication.run(args);
}
Banner.Mode.CONSOLE 這個配置有OFF、CONSOLE、LOG三種模式,這個應該很好理解,OFF就是關閉,不列印banner,列印出來就是空的,CONSOLE就是控制臺列印,是通過System.out標準輸出流列印到控制臺上的,LOG就是會呼叫logger列印info級別的日志輸出,小伙伴們可以自己去嘗試一下,
四、修改banner里面的版本號
在SringBoot啟動的時候會列印SpringBoot的版本號,這個同樣是在banner.txt檔案中設定,在banner.txt檔案中加入以下代碼,即可顯示自己專案的版本號和Springboot專案的版本號,AnsiColor主要是設定顏色,
${AnsiColor.BRIGHT_GREEN}
Project Version: ${project-name.version} ${project-name.formatted-version}
Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}
${AnsiColor.BLACK}
其中${programName.version} ${programName.formatted-version}是在yml檔案中設定的,設定如下:配置里的version取的是pom檔案中的version標簽,
project-name:
version: @version@
formatted-version: (v@version@)
原文鏈接:https://monkey.blog.xpyvip.top/archives/springboot-zi-ding-yi-banner-xin-xi
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/517553.html
標籤:其他
