Spring Boot 2 (二):Spring Boot 2 動態 Banner

Spring Boot 2 (二):Spring Boot 2 動態 Banner

Spring Boot 2.0 提供了不少新特性,其中就有一個小彩蛋:動態 Banner。php

1、配置依賴

使用 Spring Boot 2.0 首先須要將項目依賴包替換爲剛剛發佈的 2.0 RELEASE,如今網站https://start.spring.io/也將 Spring Boot 2.0 設置爲默認版本。spring

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.0.RELEASE</version>
</parent>

設置完畢後,dependencies中沒有指明版本的依賴包,將自動使用2.0.0.RELEASE依賴的版本。spring-boot

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
</dependencies>

Spring Boot 2.0 剛剛發佈,一些 Maven 倉庫還沒更新,若是導入項目後依賴包不能下載,能夠手動添加 Spring Boot 官方 Maven 倉庫。工具

<repositories>
    <repository>
        <id>spring-milestones</id>
        <name>Spring Milestones</name>
        <url>https://repo.spring.io/libs-milestone</url>
        <snapshots>
            <enabled>false</enabled>
        </snapshots>
    </repository>
</repositories>

注意:第一次使用 Spring Boot 2.0 ,完整依賴包須要下載半小時左右。測試

2、Spring Boot 更換 Banner

咱們先來回顧一下在 Spring Boot 1.0 中如何更換啓動 Banner,其實都很簡單,只須要在src/main/resources路徑下新建一個banner.txt文件,banner.txt中填寫好須要打印的字符串內容便可。動畫

通常狀況下,咱們會藉助第三方工具幫忙轉化內容,如網站http://www.network-science.de/ascii/將文字轉化成字符串,網站:http://www.degraeve.com/img2txt.php能夠將圖片轉化成字符串。網站

以Hello World爲啓動字符串來進行測試:url

.__ .__ .__ .__ .___ |  |__   ____ |  | |  |   ____   __  _  _____________|  |    __| _/
|  |  \_/ __ \|  | |  |  /  _ \  \ \/ \/ /  _ \_  __ \  |   / __ | 
|   Y  \  ___/|  |_|  |_(  <_> )  \     (  <_> )  | \/  |__/ /_/ | 
|___|  /\___  >____/____/\____/    \/\_/ \____/|__|  |____/\____ | \/     \/                                                  \/

在 Spring Boot 2.0 項目src/main/resources路徑下新建一個banner.txt文件,粘貼上述字符串,啓動 Spring Boot 項目觀察啓動日誌,發現 Spring Boot 2.0 已經將默認的 Spring 字符串替換爲 hello world。說明 Spring Boot 2.0 也支持1.0更換 Banner 的方法。spa

接下來在 Spring Boot 2.0 項目中測試打印動態 Banner ,使用的gif以下:.net

 

一樣咱們將 banner.gif 文件放到項目的src/main/resources路徑下,啓動項目進行測試,輸出欄打印信息以下:

 .. .::* ... ..... .... ........ . ... ........ . .. ...... .... .... ... . . ..**::**.. .*::::::::::*. .*::::::::::::*. .*::::::::::::*. .*::::::::::::*. .*::::::::::*. ..**::***. .. ..... .. ..... ... ...... ...... . ... . ..... . .... . . ............................ ............. .................. ......... ................ . ................... ... . ... ............ ............. . ... ............... . ......... ........... .......... ....... .... ............ ........ ........ ........ .... . ......... ........ ........ ......... ..********..                      ......*.. ........ .**::::::::::::**. ........ ........... .**::::::::::::::::**. ....... ...... .*::::::::::::::::::::*. ....... ... .. .*::::::::::::::::::::::*. ....... ...... .::::::::::::::::::::::::. ....... .......... .::::::::::::::::::::::::. ... .... ......... .*:::::::::::::::::::::::. .... ......... .*::::::::::::::::::::::*. ... ......... .*::::::::::::::::::::*. .... ........ .**::::::::::::::::**. ......... ... .... .**::::::::::::**. ......... . ........ .********.. ......... ....... . ......*.. ......... . ..... ....... ......... ........ . ............ ............ .. ........... . ............. ......... ................ .... .......... ............. .... . ......... . ..... . .... ...... ......... . .. .... .............. .... .. ............. ........... .............. ::. .*:* :. *:*      *. .*:* .:*.    *:   .*:*. :. .: :* :     :.                .::::* : : : *         *****.....       *.: : : : .:*         .::::::::::**..     ..  : * : *:           .*:::::::::::::::*.     *   ** o ***        ...**::::::::::::::::::**.   * :.: : .* :    .....***::::::::::::::::::::::*. : : o :*.  *   ..*****:::::::::::::::::::::::::*.   * : o : . .*::::::::::::::::::::::::::::::::*.    ::   * :*   :   *::::::::::::::::::::::::::::::::::* : o * o  *   .*::::::::::::::::::::::::::::::::::*. .:o : : : : .*::::::::::::::::::::::::::::::::::*. : . : : .. .*::::::::::::::::::::::::::::::::::*.   *  : * : : .*::::::::::::::::::::::::::::::::::*   :   *: . :*    .*o:::::::::::::::::::::::::::::::*. . : : : *   .*::::::::::::::::::::::::::::::*.   *   :* o *   :   .*::::::::::::::::::::::::::::*.    : ** : :*   *    .*::::::::::::::::::::::::**.    *..  *            
                  **   *    ..*::::::::::::::::::::*..     :*   *             
                   * o  ..     ..**::::::::::::**.. .:. : : : o.: ...******...      *. : :. : ::o:. *:     * *: :. : .*:*.   :*    .*:: .:*  .*      *o: .: .:*.        .*: ... 

經過上述輸出咱們發現 Spring Boot 在啓動的時候,會將 gif 圖片的每個畫面,按照順序打印在日誌中,全部的畫面打印完畢後,纔會啓動 Spring Boot 項目。

若是目錄src/main/resources下同時存在banner.txtbanner.gif,項目會先將banner.gif每個畫面打印完畢以後,再打印banner.txt中的內容。

項目的啓動 Banner 有什麼用呢,在一些大的組織或者公司中,能夠利用這個特性定製本身專屬的啓動畫面,增長團隊對品牌的認同感。

相關文章
相關標籤/搜索