Spring Boot 定制自己的 Banner

本文将介绍怎样在 Spring Boot 中自定义 Banner,通常我们习惯将 Banner 定义为自己项目名称。

Spring Boot 在启动时,默认会输出一个用字符拼接的“Spring”图,包含了 Spring Boot 的版本信息。如下:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.4.RELEASE)

Spring Boot 为了实现上面的效果,专门提供了一个 Banner 接口,该接口源码如下:

package org.springframework.boot;

import java.io.PrintStream;
import org.springframework.core.env.Environment;

/**
 * 用于以编程方式编写横幅的接口类
 * @since 1.2.0
 */
@FunctionalInterface
public interface Banner {

   /**
    * 将横幅打印到指定的打印流。
    * @param environment Spring 环境
    * @param sourceClass 应用的 class 对象
    * @param out 输出打印流
    */
   void printBanner(Environment environment, Class<?> sourceClass, PrintStream out);

   /**
    * 用于配置 Banner 的可能值的枚举。
    */
   enum Mode {
      /* 禁止打印横幅 */
      OFF,
      /* 将横幅打印到 System.out,标准输出设备 */
      CONSOLE,
      /* 将横幅打印到日志文件 */
      LOG
   }
}

Spring Boot 为 Banner 提供了如下几个实现类:

  • SpringBootBanner:默认 Banner 实现,它将输出“ Spring”横幅

  • ResourceBanner:文本格式,SpringBoot 会读取 classpath 下的 banner.txt 文件或配置指定的文件(这用于自定义banner文件名称);spring.banner.location 优先 banner.txt。

  • ImageBanner:图片格式,SpringBoot 加载配置项指定的图片,然后将图片转成 ASCII 图作为横幅打印。如果你没有配置 banner.image.location,转而依次加载类路径下的 banner.gif、banner.jpg、 banner.png 这三个中存在的某一个文件。

注意:配置文件中指定的 banner 或者 banner 图片优先 classpath 下面创建的 banner.txt 或 banner.png 资源。

生成工具

文章开始看见的“Spring”字符图,是使用工具生成的。如果让我们手工的来编辑这些字符文字,显然是一件非常困难的差事。所以,我们可以借助下面这些工具,轻松地根据文字或图片来生成用于 Banner 输出的字符文字。

定义自己的Banner

(1)在 Spring Boot 项目的 resources 目录下面创建一个 banner.txt 文件,Spring Boot 会自动读取该文件的内容。将你要显示的 Banner 写入该文件即可,如下:

 _    ___   __ _____ _______ _____  _______      ________
| |  | \ \ / // ____|__   __|  __ \|_   _\ \    / /  ____|
| |__| |\ V /| (___    | |  | |__) | | |  \ \  / /| |__
|  __  | > <  \___ \   | |  |  _  /  | |   \ \/ / |  __|
| |  | |/ . \ ____) |  | |  | | \ \ _| |_   \  /  | |____
|_|  |_/_/ \_\_____/   |_|  |_|  \_\_____|   \/   |______|

                                  https://www.hxstrive.com

(2)启动你的 Spring Boot 即可输出自定义的 Banner 了,如下:

  _    ___   __ _____ _______ _____  _______      ________
 | |  | \ \ / // ____|__   __|  __ \|_   _\ \    / /  ____|
 | |__| |\ V /| (___    | |  | |__) | | |  \ \  / /| |__
 |  __  | > <  \___ \   | |  |  _  /  | |   \ \/ / |  __|
 | |  | |/ . \ ____) |  | |  | | \ \ _| |_   \  /  | |____
 |_|  |_/_/ \_\_____/   |_|  |_|  \_\_____|   \/   |______|

                                   https://www.hxstrive.com
2020-10-17 17:26:01.033  INFO 2396 --- [  restartedMain] c.h.s.s.SpringbootBannerApplication      : Starting SpringbootBannerApplication on MS-OYCYMLXUSLLD with PID 2396 (D:\learn\Spring\springboot\springboot_workspaces\springboot2\springboot_banner\target\classes started by Administrator in D:\learn\Spring\springboot\springboot_workspaces\springboot2\springboot_banner)
2020-10-17 17:26:01.038  INFO 2396 --- [  restartedMain] c.h.s.s.SpringbootBannerApplication      : No active profile set, falling back to default profiles: default
2020-10-17 17:26:01.133  INFO 2396 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2020-10-17 17:26:01.134  INFO 2396 --- [  restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG'
2020-10-17 17:26:04.143  INFO 2396 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-10-17 17:26:04.202  INFO 2396 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-10-17 17:26:04.202  INFO 2396 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.17]
2020-10-17 17:26:04.395  INFO 2396 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-10-17 17:26:04.395  INFO 2396 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3261 ms
2020-10-17 17:26:04.779  INFO 2396 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-10-17 17:26:04.996  INFO 2396 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-10-17 17:26:05.074  INFO 2396 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-10-17 17:26:05.080  INFO 2396 --- [  restartedMain] c.h.s.s.SpringbootBannerApplication      : Started SpringbootBannerApplication in 4.718 seconds (JVM running for 7.606)

当然,上面这波操作是最快速和最简单的。你不一定非要将文件命名为 banner.txt,完全可以取你自己喜欢的名称。然后使用如下配置进行指定:

# 属性文件配置方式
spring.banner.location=haha.txt

或者

# yaml 格式文件配置方式
spring:
  banner:
    location: haha.txt

关于使用图片作为 banner,读者可以自行尝试,配置如下:

spring.banner.image.location=banner.png

或者

spring:
  banner:
    image:
      location: banner.png
学习从来无捷径,循序渐进登高峰。 —— 高永祚
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号