9

Spring Boot 3.0 (十八):Spring Boot 动态 Banner

 1 year ago
source link: http://www.ityouknow.com/springboot/2023/01/18/spring-boot-banner.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Spring Boot 3.0 (十八):Spring Boot 动态 Banner

2023/01/18

Spring Boot 提供了很多新特性,其中就有一个小彩蛋:动态 Banner,今天我们就先拿这个来尝尝鲜。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>3.0.0</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

设置完毕后,dependencies中没有指明版本的依赖包,将自动使用相关依赖的版本。

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

如一些 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 更换 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.png

我们以Hello World为启动字符串来进行测试:

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

在 Spring Boot 3.0 项目src/main/resources路径下新建一个banner.txt文件,粘贴上述字符串,启动 Spring Boot 项目观察启动日志,发现 Spring Boot 3.0 已经将默认的 Spring 字符串替换为 hello world。说明 Spring Boot 3.0 也支持1.0更换 Banner 的方法。

接下来在 Spring Boot 3.0 项目中测试打印动态 Banner ,使用的gif如下:

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.txtbanner.gif,项目会先将banner.gif每一个画面打印完毕之后,再打印banner.txt中的内容。

项目的启动 Banner 有什么用呢,在一些大的组织或者公司中,可以利用这个特性定制自己专属的启动画面,增加团队对品牌的认同感。

注意,如此操作在ide中无法实现动图动态展示,我们需要在命令行中启动项目,两种方式都可以: 1.切换到项目目录中 mvn springboot:run 2.切换到项目目录中 mvn install ,切换到 target目录 java -jar 打包好的项目

大功告成! ———————————————— 版权声明:本文为CSDN博主「hunhun1122」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/hunhun1122/article/details/89228138

示例代码-github

示例代码-码云

demo-animated-banner

Show Disqus Comments

Related Issues not found

Please contact @ityouknow to initialize the comment


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK