【从零开始学Spring Boot】-6.Spring Boot Banner自定义


1.简介

1.1 概述

The banner that is printed on start up can be changed by adding a banner.txt file to your classpath or by setting the spring.banner.location property to the location of such a file. If the file has an encoding other than UTF-8, you can set spring.banner.charset. In addition to a text file, you can also add a banner.gif, banner.jpg, or banner.png image file to your classpath or set the spring.banner.image.location property. Images are converted into an ASCII art representation and printed above any text banner.

banner 是在启动时打印的内容,可以通过在 classpath 下添加 “ banner.txt” 文件或 设置“ spring.banner.location” 属性值来改变 banner 的内容。如果文件的编码不是UTF-8,则可以通过spring.banner.charset 进行设置。除了文本文件之外,您还可以在 classpath 下添加 banner.gifbanner.jpgbanner.png 图像文件或设置 spring.banner.image.location 属性。图片将被转换为 ASCII 的形式进行展现并在文本内容之前打印。

1.2 特点

banner.txt 可以包含如下内容

Variable Description
${application.version} The version number of your application, as declared in MANIFEST.MF. For example, Implementation-Version: 1.0 is printed as 1.0.
${application.formatted-version} The version number of your application, as declared in MANIFEST.MF and formatted for display (surrounded with brackets and prefixed with v). For example (v1.0).
${spring-boot.version} The Spring Boot version that you are using. For example 2.2.8.RELEASE.
${spring-boot.formatted-version} The Spring Boot version that you are using, formatted for display (surrounded with brackets and prefixed with v). For example (v2.2.8.RELEASE).
${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME}) Where NAME is the name of an ANSI escape code. See AnsiPropertySource for details.
${application.title} The title of your application, as declared in MANIFEST.MF. For example Implementation-Title: MyApp is printed as MyApp.

The SpringApplication.setBanner(…) method can be used if you want to generate a banner programmatically. Use the org.springframework.boot.Banner interface and implement your own printBanner() method.

You can also use the spring.main.banner-mode property to determine if the banner has to be printed on System.out (console), sent to the configured logger (log), or not produced at all (off).

如果您要以编程方式生成 banner,则可以使用 SpringApplication.setBanner(…) 方法。使用org.springframework.boot.Banner 接口并实现自己的 printBanner() 方法。您还可以使用 spring.main.banner-mode 属性来确定横幅是否必须在 System.out(控制台)上打印,发送到配置的 logger(日志)或者完全不生成(关闭)。

2.演示环境

  1. JDK 1.8.0_201
  2. Spring Boot 2.2.0.RELEASE
  3. 构建工具(apache maven 3.6.3)
  4. 开发工具(IntelliJ IDEA )

3.演示代码

3.1 代码说明

自定义 banner.txt,在项目启动时打印自定义 banner

3.2 代码结构

3.3 maven 依赖

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

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

3.4 配置文件

application.properties

application.version=0.0.1-SNAPSHOT
application.formatted-version=0.0.1
application.title=spring-boot-banner

banner.txt

${application.version}
${application.formatted-version}
${application.title}
${AnsiColor.BRIGHT_GREEN}$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
${AnsiColor.BRIGHT_CYAN}$$       _________  __  __/ / /_  ____ _/ / /___ _____/ /    $$
${AnsiColor.BRIGHT_CYAN}$$      / ___/ __ \/ / / / / __ \/ __ `/ / / __ `/ __  /     $$
${AnsiColor.BRIGHT_CYAN}$$     (__  ) /_/ / /_/ / / /_/ / /_/ / / / /_/ / /_/ /      $$
${AnsiColor.BRIGHT_CYAN}$$    /____/\____/\__,_/_/_.___/\__,_/_/_/\__,_/\__,_/       $$
${AnsiColor.BRIGHT_GREEN}$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
               ${AnsiColor.BRIGHT_RED}Spring Boot: ${spring-boot.formatted-version}

3.6 git 地址

spring-boot/spring-boot-05-basis/spring-boot-banner

4.效果展示

启动 SpringBootBannerApplication.main 方法,打印出 banner.txt 中内容

5.参考

  1. 官方文档-Spring Boot Features/Banner

文章作者: Soulballad
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Soulballad !
评论
 上一篇
【源码分析-Spring Boot】-6.Spring Boot Banner 是如何打印的 【源码分析-Spring Boot】-6.Spring Boot Banner 是如何打印的
Spring Boot Banner打印:【从零开始学Spring Boot】-6.Spring Boot Banner自定义 1.banner 是如何打印的?banner 是如何打印的呢? 在 spring boot 项目启动时,调用
2020-07-13
下一篇 
【源码分析-Spring Boot】-5.Spring Boot Profile 是如何生效的 【源码分析-Spring Boot】-5.Spring Boot Profile 是如何生效的
Spring Boot Profile 环境隔离: 【从零开始学Spring Boot】-5.Spring Boot Profile环境隔离 1.Spring Boot 中 Profile 是如何生效的?在启动类启动的时候,按照如下顺序
2020-07-12
  目录