SpringBoot复习:(13)Banner是怎么打印出来的?

SpringApplication的run方法代码:

复制代码
	public ConfigurableApplicationContext run(String... args) {
		long startTime = System.nanoTime();
		DefaultBootstrapContext bootstrapContext = createBootstrapContext();
		ConfigurableApplicationContext context = null;
		configureHeadlessProperty();
		SpringApplicationRunListeners listeners = getRunListeners(args);
		listeners.starting(bootstrapContext, this.mainApplicationClass);
		try {
			ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
			ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
			configureIgnoreBeanInfo(environment);
			Banner printedBanner = printBanner(environment);
			context = createApplicationContext();
			context.setApplicationStartup(this.applicationStartup);
			prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
			refreshContext(context);
			afterRefresh(context, applicationArguments);
			Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
			if (this.logStartupInfo) {
				new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);
			}
			listeners.started(context, timeTakenToStartup);
			callRunners(context, applicationArguments);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, listeners);
			throw new IllegalStateException(ex);
		}
		try {
			Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
			listeners.ready(context, timeTakenToReady);
		}
		catch (Throwable ex) {
			handleRunFailure(context, ex, null);
			throw new IllegalStateException(ex);
		}
		return context;
	}

其中包含如下代码片段:

printBanner的代码如下:

SpringApplication的bannerMode成员变量默认值为CONSOLE,所以会执行

return bannerPrinter.print(environment, this.mainApplicatonClass, System.out);

print方法代码如下:

调用的getBanner代码如下:

其中getImageBanner和getTextBanner分别从约定好的位置读取图片banner和文本banner,如果我们没有配置,则都为null.最后该方法返回默认的banner,也就是DEFAULT_BANNER,该常量为:

也就是返回一个SpringBootBanner。接下来调用banner.printBanner方法:

SpringBootBanner类的printBanner代码如下:

这个printBanner方法最终拼接打印出我们在控制台看到的效果

相关推荐
开开心心就好4 分钟前
系统管理工具,多功能隐私清理文件粉碎工具
java·网络·windows·r语言·电脑·excel·symfony
小杨同学498 分钟前
C 语言贪心算法实战:解决经典活动选择问题
后端
+VX:Fegn08958 分钟前
计算机毕业设计|基于springboot + vue物流配送中心信息化管理系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·小程序·课程设计
随丶芯9 分钟前
IDEA安装leetcode-editor插件
java·开发语言
范什么特西14 分钟前
下载idea旧版本
java·ide·intellij-idea
计算机毕设指导621 分钟前
基于微信小程序的钓鱼论坛系统【源码文末联系】
java·spring boot·mysql·微信小程序·小程序·tomcat·maven
qq_124987075322 分钟前
基于微信小程序的宠物交易平台的设计与实现(源码+论文+部署+安装)
java·spring boot·后端·微信小程序·小程序·毕业设计·计算机毕业设计
小毅&Nora22 分钟前
【Java线程安全实战】⑧ 阶段同步的艺术:Phaser 与 Condition 的高阶玩法
java·多线程
内存不泄露23 分钟前
基于Spring Boot和Vue的企业办公自动化系统设计与实现
java·vue.js·spring boot·intellij-idea
禹曦a24 分钟前
Java实战:Spring Boot 构建电商订单管理系统RESTful API
java·开发语言·spring boot·后端·restful