天天看點

Spring boot 2.0 新特性之動态 Banner

版權聲明:本文為部落客原創文章,未經部落客允許不得轉載。 https://blog.csdn.net/xmt1139057136/article/details/79431643

Spring Boot 2.0 提供了很多新特性,其中就有一個小彩蛋:動态 Banner,今天我們就先拿這個來嘗嘗鮮。

配置依賴

使用 Spring Boot 2.0 首先需要将項目依賴包替換為剛剛釋出的 2.0 RELEASE,現在網站

https://start.spring.io/

也将 Spring Boot 2.0 設定為預設版本。

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

設定完畢後,

dependencies

中沒有指明版本的依賴包,将自動使用2.0.0.RELEASE依賴的版本。

<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 ,完整依賴包需要下載下傳半小時左右。

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為啟動字元串來進行測試:

.__           .__  .__                               .__       .___
|  |__   ____ |  | |  |   ____   __  _  _____________|  |    __| _/
|  |  \_/ __ \|  | |  |  /  _ \  \ \/ \/ /  _ \_  __ \  |   / __ | 
|   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 的方法。

接下來在 Spring Boot 2.0 項目中測試列印動态 Banner ,使用的gif如下:

同樣我們将 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.txt

banner.gif

,項目會先将

banner.gif

每一個畫面列印完畢之後,再列印

banner.txt

中的内容。

項目的啟動 Banner 有什麼用呢,在一些大的組織或者公司中,可以利用這個特性定制自己專屬的啟動畫面,增加團隊對品牌的認同感。

業餘草

分享100套精選1000G架構師資料課程(超1T的IT學習資料免費送)。

超過1024G的IT學習資料免費領取,你值得擁有!

領取資源方式,關注“業餘草”公衆号,回複對應的關鍵字

  • 01、回複”我要健康“,擷取程式員健康、内涵大禮包!
  • 02、回複”大資料“,擷取大資料相關資源。
  • 03、回複”linux“,擷取linux相關資料。
  • 04、回複”mongodb“,擷取mongodb學習資料。
  • 05、回複”redis“,擷取redis相關資料。
  • 06、回複”zookeeper“,擷取分布式等相關資料。
  • 07、回複”dubbo“,擷取dubbo相關資料。
  • 08、回複”git“,擷取git資料
  • 09、回複”設計模式“,擷取設計模式資料
  • 10、回複”小程式“,擷取小程式相關的資料
  • 11、回複”android“,擷取安卓相關的資料
  • 12、回複”java“,擷取java相關的資料
  • 13、回複”pyhton“,擷取Pyhton相關資料。
  • 14、回複”springboot“,擷取Spring Boot相關資料。
  • 15、回複”springcloud“,擷取Spring Cloud相關資料。

本文源代碼下載下傳:https://github.com/ityouknow/spring-boot-examples