螺竹编程
发布于 2024-05-04 / 15 阅读
1

SpringBoot基础/配置:banner的配置

介绍

Spring Boot 提供了自定义启动时的 Banner(横幅)配置,它是在应用程序启动时显示的 ASCII 艺术图案或自定义文本。你可以通过在配置文件中添加一些属性来进行配置。

以下是一些常用的 Spring Boot Banner 配置选项:

  1. banner.charset:指定 Banner 文件的字符集,默认为 UTF-8。

spring.banner.charset=UTF-8
  1. banner.location:指定 Banner 文件的路径。可以是文件系统路径(file:)或类路径(classpath:)。如果指定的路径无效,则会回退到默认的 Spring Boot 横幅。

spring.banner.location=classpath:my-banner.txt
  1. banner.image.location:指定自定义图片横幅的路径。图片横幅可以是 PNG、JPEG 或 GIF 格式的图片文件。

spring.banner.image.location=classpath:my-banner.png
  1. banner.image.width:指定图片横幅的宽度。默认为 76。

spring.banner.image.width=100
  1. banner.image.height:指定图片横幅的高度。默认为 -1,表示保持原始高度。

spring.banner.image.height=50
  1. banner.image.margin:指定图片横幅的边距。默认为 2。

spring.banner.image.margin=5
  1. banner.text:指定自定义文本横幅的内容。可以使用 ASCII 艺术字体生成器生成艺术字,并将其指定为横幅文本。

spring.banner.text=My Custom Banner
  1. banner.location.order:指定 Banner 配置的位置查找顺序。默认为META-INF/spring-banner.txt,classpath:banner.txt

spring.banner.location.order=classpath:my-banner.txt,META-INF/spring-banner.txt

以上配置属性可以在 Spring Boot 配置文件(如 application.properties 或 application.yml)中进行设置。你可以根据需要选择适合你应用程序的 Banner 配置方式。

示例

项目地址:CodeCoderCoding/Spring-Boot-Demo: Spring-Boot的Demo。Spring-Boot基础、Spring-Boot与数据库、Spring-Boot与安全、Spring-Boot与消息队列 (github.com)

项目结构

SpringBootConfigBannerApplication

import org.springframework.boot.Banner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;

@SpringBootApplication
public class A02SpringBootConfigBannerApplication {

    public static void main(String[] args) {
        SpringApplicationBuilder builder = new SpringApplicationBuilder(A02SpringBootConfigBannerApplication.class);
        builder.bannerMode(Banner.Mode.OFF).run(args);
    }

}

banner.txt

#####  ###### #    # #####  ###### #    #      ####  #    # 
#    # #      ##  ## #    # #      #    #     #    # ##   # 
#####  #####  # ## # #####  #####  #    #     #      # #  # 
#    # #      #    # #    # #      # ## # ### #      #  # # 
#    # #      #    # #    # #      ##  ## ### #    # #   ## 
#####  ###### #    # #####  ###### #    # ###  ####  #    #