SpringBoot使用动态Banner

本文介绍了如何在SpringBoot2.0项目中使用动态Banner,包括添加POM依赖、创建启动类,以及如何通过文本文件或GIF图片自定义启动时的日志输出。此外,还提到在大型组织中利用此功能增强品牌识别度。
摘要由CSDN通过智能技术生成

SpringBoot使用动态Banner

Spring Boot 2.0 提供了很多新特性,其中就有动态 Banner。

1、pom依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>spring-boot-banner</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-boot-banner</name>
    <description>spring-boot-banner</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

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

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

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2、启动类

package com.example;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);
    }

}

3、更换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路径下,启动项目进行测试,输出栏打印信息如

下:

                                           .,ijtjjjjtti,.                                           
                                        ijttttttjjttttttttji                                        
                                     ;tjttttttttttjjjjttjtjjjj;                                     
                                   jjtttttjttjtttttijjittjjttttjj.                                  
                                .ijttttjittjjtttjtjjtttjtjtttjtjjjt.                                
                               ijttttttjttjjttttjttitttttjijtjjttttji.          .:                  
                             itttttttttjtttijjttttttttttjtttttjtjttttti         tt                  
                           .jttttttttttjtjtttjjttttjtjjttjjtttttttjjttjj:       jt                  
                          ,tttttttttttjttttjttjttjtjtjtttttjtjtttttjttttj;     ,tt,                 
                         ;jtttttjttttttttitttttttttttttjttttttttttjjttjjtji    j:jt                 
                        ,jttjjttttjtttjjtttjtjjtjttjijtitjjjttttttjtjtjjiti,  ij tt                 
                       ttjjtttttttttttjttjjtttjtttjjtttjjtjtttjjttjjjtjtitttttj: .j,                
                      tjjjittjtttttttjtjjtjtttttttttjjtjjtjttttttjjjjjtjjjtttt;   jt                
                     itjttttjttttttjtttjttjttjttttjtjtjjtjjjjitijtttjtjjtjtttt    it                
                    ;jtttjtttttttttttttttttttjttjtttttttjjjtttttjtjittjtjjttt     ,j:               
                    jjtjtttttjttttttjttttttttjjtttttttttttjjjttiijtjtjttittj.      ji               
                   ;tjtttttttttjtttttjttttttttjjjtjjtttttjjtjttttjtititjttt.       tj               
                  ;tjjtjtttttttttttttttttttjtttttjjtttttjjtttjjjtjttjtjtjt         ,t               
                  jittttjtttjtttttttttjttttjttttttjjttttjjtjtjijittjtttj;          .j.              
                 tjtjtttttjttjttttjtttjttjtttttttitttttttjttjtttjtttjtt:            t:              
                :tttttttjtttttjttjjjjttttttttjjjtttttttttjttjjjjtttttj              ii              
                ttttjtjtttjjttjttttjttttttttttttjtttjjtttttttttjttjji               :j              
               ittttjtttjjttjtjtjttjtttjjttttjtttjttttttjtttttjtji                   j.             
               jtttttijtjjttjtjttitjtttttttttjijtjjjttttttttitjt:                    t,             
               ttttttttijjitttjjtjjtjtjtttttjjjttittjtttjtjjt,                       ji             
              ,tjjtttjtjjjjttjttijjtitttttttjtjtttttittjji,.                         tt             
              tjttjjjtjttititjtttttjjijjtjtjjtttii;,:.                               tj             
              jjjjjtjjjjtjttjjttijittjjj;:                                           ;j.            
             ,jjjtttttjtjttttttittjt:                                                :t,            
             ttijtjtjtjtjtjtttjjtj;                                          :        j;            
             jtttjtjjjtjtttttjtjt                                            .        ji            
            .tttjjjtjtjtttttttt,                                            ;         jt            
            :tttjtttttttjttttj                                              i         jj            
            ;ttjjtjijttjtjttj.                                             t.         tj.           
            ittjittjjttttttj.                                              t          tj.           
            tttjjjtjttttttj:                                              ;:          tj.           
            ttttttttittttti                                              .j           ij.           
            tjtjtjjttttttj.                                              jt           tj.           
            tjtjjjjttjjtj.                                             .jt            tj.           
            jttttttttjttj                                              tt;            tj.           
            tjtttjtttttjt                                             tjt             tj.           
            ttttttttjttt;                                             jt:             jt            
            iijjtttttttt.                                            jj;              jt            
            ;jtjtjtttjtj                                            tjj               ji            
            :tijjtjjtjjt                                          tjtj               ;j,            
            .jttttjtjiji                                         tttj;               tj.            
             jtjjtttttj;                                        ijtti                jj.            
             tttjtjttjj;                                      .tjtjt                 tt             
             ;jjjtttttji                                     :jtjji                 .ti             
              jtttttttjt                                    jjttj:                  tj.             
              ttttijtjjj                                  ;jtjjti                   jt              
              :jtttjtttt.                               ;tjtjtj;                   ij,              
               ttijtttjj;                             .tjttttj,                    jj.              
               jtjjtttttt                           ,tjttjjtj                     ttt               
               ;jttttttjt                         ijjtttjjtj                     :jjt               
                tjjjttjtti                   :ijjjtjjttjjt:                     :jjj                
                :ttttttttj,               ,ijttttjtjtijj,                       jtj;                
                 tjtjttjtjj:          :;jjtjtjttitjttjt                        itjt                 
                  ttjttttjtj;     .ijjttttjjttttttjji                         tjjt:                 
                  ;ittjjtjtttj,tjjttjttttttttjjtjj,                          jtjjt                  
                   ;tttjjtjttttjtttttjjtttttjt;                           .jjjjtt                   
                    jttiji. ;ttjjjtttttttjt;                            :tjttttj                    
                    ;tjjt     jtjjijtttj;                             ;jttttttti                    
                     ijt:     jtitttji.                           :;jjjtjtttttt                     
                      tj      jtttt::;iiitiitii;;;;,,,,,,,,;iitjjjttttjttjjttt.                     
                       t      tjttttjjjtttttttjjjtttjtttjtttttjjjjttjtjtjjjtj.                      
                        :     tittjtttjtjtjjjtjitttjtjttjjtjttjjtjjttjjtjtj,                        
                         :  :jtttjtttjjjtttttttjtjtttttjjjtttjjjttjttjjtttt                         
                          ,jttttjjtjjjjtitjtjttjjjtjtjttitjttjttttjjtjjjj;                          
                            jjittjtjtjjjjjtttjtttjttjtttttjjttjjjtttttjt.                           
                             ;ttijtjttjtjttttjjjtttttjjtjttijtjttttjtji                             
                               itttjtjtjtttttttttjtttjjttjtjjjttttijt.                              
                                .ijjtjttjjtjtjittttttittjjjjjjttttt.                                
                                   tjjtjjtjttjtjjtjtjttjtttjtjtjt.                                  
                                     ;jjttjjjtttittttitttttttji                                     
                                        itjjtttjjjtitttjtjji                                        
                                           .,ittjjjjtti,:                                 

通过上述输出我们发现 Spring Boot 在启动的时候,会将 gif 图片的每一个画面,按照顺序打印在日志中,所有的

画面打印完毕后,才会启动 Spring Boot 项目。

如果目录src/main/resources下同时存在banner.txtbanner.gif,项目会先将banner.gif每一个画面打

印完毕之后,再打印banner.txt中的内容。

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

团队对品牌的认同感。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值