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.txt
和banner.gif
,项目会先将banner.gif
每一个画面打
印完毕之后,再打印banner.txt
中的内容。
项目的启动 Banner 有什么用呢,在一些大的组织或者公司中,可以利用这个特性定制自己专属的启动画面,增加
团队对品牌的认同感。