Springboot启动画面「banner」显示设置与源码解析

基于springboot版本:2.2.2.RELEASE

Springboot启动图案的修改,网上教程很多,这里不再多说

修改这个图案的方法如下:

在resources/目录下创建名为banner.txt的文件,banner.txt里写入需要显示的内容即可

内容可从 http://patorjk.com/software/taag/  制作

这里主要是想记录一下如何关闭这个的显示:

方法一:删除banner.txt 内容

方法二:启动时设置

SpringApplication app = new SpringApplication(DemoApplication.class);
		app.setBannerMode(Banner.Mode.OFF);
		app.run(args);

方法三:配置文件修改:

spring:
  main:
    banner-mode: off

扩展一下:

Banner.Mode可选值

@FunctionalInterface
public interface Banner {
	void printBanner(Environment environment, Class<?> sourceClass, PrintStream out);
	enum Mode {

		/**
		 * Disable printing of the banner.
		 */
		OFF,

		/**
		 * Print the banner to System.out.
		 */
		CONSOLE,

		/**
		 * Print the banner to the log file.
		 */
		LOG

	}

}
spring.main.banner-mode可选值: {off | console | log}

研究一下:

Springboot自动生成时默认的启动入口:SpringApplication.run(DemoApplication.class, args);

download源码看一下SpringApplication.java 可以看到

从入口看banner打印过程:

1. 启动

2. 配置初始化

3. 影响因素:

分析:

  只有banner.mode=off的情况下才不会生效(对应方法二、三)

在哪打印的呢?研究一下bannerPrinter.print () 这个方法

这里有个注意事项,当banner.txt 文件编码不为utf-8时这里有解决方案

关于banner.txt,可以参考{bannerPrinter.print () 中}:

Banner banner = getBanner(environment);

banner.txt路径定义在:

org.springframework.boot.SpringApplicationBannerPrinter.DEFAULT_BANNER_LOCATION

参考资料(未经本人验证)

亲手实现banner图案:https://www.cnblogs.com/niumoo/p/10434746.html

banner制作(图片及文字):https://www.jianshu.com/p/e75c73e6b286

发布了15 篇原创文章 · 获赞 14 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/master336/article/details/103794438