您的位置:首页 > 编程语言 > Java开发

Spring Boot 2.0(二):Spring Boot 2.0尝鲜-动态 Banner

2018-03-03 17:14 549 查看
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 有什么用呢,在一些大的组织或者公司中,可以利用这个特性定制自己专属的启动画面,增加团队对品牌的认同感。

示例代码-github

示例代码-码云

参考

demo-animated-banner
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: