首页 > 编程语言 >SpringBoot源码 | printBanner方法解析

SpringBoot源码 | printBanner方法解析

时间:2022-12-09 14:06:25浏览次数:35  
标签:printBanner resource SpringBoot Banner resourceLoader environment 源码 return bann


SpringBoot源码 | printBanner方法解析

  • ​​printBanner​​
  • ​​bannerPrinter.print​​
  • ​​getBanner​​
  • ​​getImageBanner​​
  • ​​getTextBanner​​

printBanner

printBanner方法用于打印在src/main/resources下名字是banner的自定义日志文件信息,对于整体的SpringBoot启动流程来说不算主启动业务流程,但是也提供了自定义打印日志内容的可能,有一定存在的意义,所以这里也一起来看一下printBanner方法内部吧,printBanner方法源码加入注释后

private Banner printBanner(ConfigurableEnvironment environment) {
//Disable printing of the banner 是否允许打印banner信息
if (this.bannerMode == Banner.Mode.OFF) {
return null;
}
//资源加载类
ResourceLoader resourceLoader = (this.resourceLoader != null) ? this.resourceLoader
: new DefaultResourceLoader(null);
//构造SpringApplicationBannerPrinter 对象
SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(resourceLoader, this.banner);
//Print the banner to the log file 打印banner到日志文件
if (this.bannerMode == Mode.LOG) {
return bannerPrinter.print(environment, this.mainApplicationClass, logger);
}
//Print the banner to System.out 打印banner到System.out
return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
}

整个方法比较简单,也比较容易理解,根据我在源码中添加的注释理解即可,这里我们主要看bannerPrinter.print方法

bannerPrinter.print

首先来看一下print方法的源码

SpringBoot源码 | printBanner方法解析_spring boot


源码先获取Banner对象,然后返回PrintedBanner对象给调用方

getBanner

getBanner根据environment获取Banner对象

SpringBoot源码 | printBanner方法解析_spring boot_02


这里在获取Banner对象的时候会涉及到getImageBanner、getTextBanner方法,返回ImageBanner或者ResourceBanner

getImageBanner

getImageBanner首先会去判断是否配置了banner路径信息,后面根据resourceLoader获取banner是图片相关后缀时返回ImageBanner对象

private Banner getImageBanner(Environment environment) {
//获取配置的spring.banner.image.location路径,如果存在则加载资源
String location = environment.getProperty(BANNER_IMAGE_LOCATION_PROPERTY);
if (StringUtils.hasLength(location)) {
Resource resource = this.resourceLoader.getResource(location);
return resource.exists() ? new ImageBanner(resource) : null;
}
//判断是否是IMAGE_EXTENSION = { "gif", "jpg", "png" }后缀,是则返回ImageBanner
for (String ext : IMAGE_EXTENSION) {
Resource resource = this.resourceLoader.getResource("banner." + ext);
if (resource.exists()) {
return new ImageBanner(resource);
}
}
return null;
}

getTextBanner

getTextBanner加载默认配置路径的banner.txt文件并判断存在性和不包含特定条件,满足则返回ResourceBanner

private Banner getTextBanner(Environment environment) {
//获取资源配置spring.banner.location默认文件名banner.txt的资源路径
String location = environment.getProperty(BANNER_LOCATION_PROPERTY, DEFAULT_BANNER_LOCATION);
//加载资源
Resource resource = this.resourceLoader.getResource(location);
try {
//如果资源存在且resource的URL不包含liquibase-core则返回ResourceBanner
if (resource.exists() && !resource.getURL().toExternalForm().contains("liquibase-core")) {
return new ResourceBanner(resource);
}
}
catch (IOException ex) {
// Ignore
}
return null;
}

如此便获取到Banner,回到print方法打印banner.txt文本内容

SpringBoot源码 | printBanner方法解析_spring boot_03


通过构造方法返回PrintedBanner用于后续打印日志信息输出

SpringBoot源码 | printBanner方法解析_java_04


其中构造方法中参数sourceClass是主程序类

SpringBoot源码 | printBanner方法解析_java_05


到这里整个printBanner的加载就完成了,结果如图

SpringBoot源码 | printBanner方法解析_spring_06


banner.txt文本内容为

Application Version: ${ruoyi.version}
Spring Boot Version: ${spring-boot.version}

// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 打印banner日志信息 //


标签:printBanner,resource,SpringBoot,Banner,resourceLoader,environment,源码,return,bann
From: https://blog.51cto.com/u_10917175/5925123

相关文章