springBoot定制自己的banner

每当我们启动springBoot项目时,控制台上总会打印这样的logo:

 .    ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

实际上是springBoot在这个地方放置了一个彩蛋,虽然没有什么实际的意义,但的确增加了不少乐趣;当我们不想要或者想要修改他时,springBoot也给了我们隐藏和修改的空间。

1. 隐藏logo

代码中隐藏:

public static void main(String[] args) {
	SpringApplication application=new SpringApplication(Application.class);
	/**
	 * OFF G关闭
	 * CLOSED 后台控制台输出,默认就是这种
     * LOG 日志输出
     */
     application.setBannerMode(Banner.Mode.OFF);
     application.run(args);
}

我们也可以设置属性,令其隐藏:

spring:
	main:
		banner-mode: off

2. 修改banner的样式

其实这个也是非常简单的,我们只要在 resources 目录下放置名为 banner.txt、banner.gif 、banner.jpg 或 banner.png 的文件,Spring Boot 会自动加载,将其作为启动时打印的 logo。

对于文本文件,Spring Boot 会将其直接输出。
对于图像文件( banner.gif 、banner.jpg 或 banner.png ),Spring Boot 会将图像转为 ASCII 字符,然后输出。

如果你使用的是 banner.txt 文件中还可以使用变量来设置字体、颜色、版本号。

变量 描述
${application.version} MANIFEST.MF 中定义的版本。如:1.0
${application.formatted-version} MANIFEST.MF 中定义的版本,并添加一个 v 前缀。如:v1.0
${spring-boot.version} Spring Boot 版本。如:2.1.1.RELEASE
${spring-boot.formatted-version} Spring Boot 版本,并添加一个 v 前缀。如:v2.1.1.RELEASE
${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME}) ANSI 颜色、字体。更多细节,参考:AnsiPropertySource
${application.title} MANIFEST.MF 中定义的应用名

推荐两个生成字符画的网站,可以将生成的字符串放入这个banner.txt 文件:

http://www.network-science.de/ascii/
http://patorjk.com/software/taag/

和banner配置有关的内容

application.properties 中与 Banner 相关的配置:

# banner 模式。有三种模式:console/log/off
# console 打印到控制台(通过 System.out)
# log - 打印到日志中
# off - 关闭打印
spring.main.banner-mode = off
# banner 文件编码
spring.banner.charset = UTF-8
# banner 文本文件路径
spring.banner.location = classpath:banner.txt
# banner 图像文件路径(可以选择 png,jpg,gif 文件)
spring.banner.image.location = classpath:banner.gif
used).
# 图像 banner 的宽度(字符数)
spring.banner.image.width = 76
# 图像 banner 的高度(字符数)
spring.banner.image.height =
# 图像 banner 的左边界(字符数)
spring.banner.image.margin = 2
# 是否将图像转为黑色控制台主题
spring.banner.image.invert = false

猜你喜欢

转载自blog.csdn.net/qq_35530330/article/details/87197284