天天看點

SpringBoot2.0實戰 | 第十四章:配置Banner實作專屬的啟動畫面

目标

實作自定義啟動畫面

操作步驟

添加依賴

引入 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 什麼的,增加參與成員對公司、項目、品牌的認同感

參考

  • http://www.ityouknow.com/springboot/2018/03/03/spring-boot-banner.html
  • https://www.cnblogs.com/huanzi-qch/p/9916784.html

繼續閱讀