SpringBoot源码 | printBanner方法解析

简介: 本文主要讲述SpringBoot启动流程源码中的printBanner()方法
+关注继续查看

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方法的源码

image.png

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

getBanner

getBanner根据environment获取Banner对象

image.png

这里在获取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文本内容

image.png

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

image.png

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

image.png

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


image.png

banner.txt文本内容为

Application Version: ${ruoyi.version}
Spring Boot Version: ${spring-boot.version}
////////////////////////////////////////////////////////////////////
//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O\  =  /O                              //
//                      ____/`---'\____                           //
//                    .'  \\|     |//  `.                         //
//                   /  \\|||  :  |||//  \                        //
//                  /  _||||| -:- |||||-  \                       //
//                  |   | \\\  -  /// |   |                       //
//                  | \_|  ''\---/''  |   |                       //
//                  \  .-\__  `-`  ___/-. /                       //
//                ___`. .'  /--.--\  `. . ___                     //
//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
//      ========`-.____`-.___\_____/___.-`____.-'========         //
//                           `=---='                              //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
//             打印banner日志信息                                  //
////////////////////////////////////////////////////////////////////
相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
7天前
|
XML Java Maven
127.【SpringBoot 源码刨析D】(五)
127.【SpringBoot 源码刨析D】
15 1
|
7天前
|
Java
127.【SpringBoot 源码刨析D】(四)
127.【SpringBoot 源码刨析D】
19 1
|
7天前
|
消息中间件 监控 数据可视化
127.【SpringBoot 源码刨析D】(三)
127.【SpringBoot 源码刨析D】
35 1
|
7天前
|
监控 Java 测试技术
127.【SpringBoot 源码刨析D】(二)
127.【SpringBoot 源码刨析D】
25 0
|
7天前
|
Java 测试技术 Spring
127.【SpringBoot 源码刨析D】(一)
127.【SpringBoot 源码刨析D】
31 0
|
7天前
|
JSON NoSQL Java
124.【SpringBoot 源码刨析C】(九)
124.【SpringBoot 源码刨析C】
18 0
|
7天前
|
XML Java 数据库连接
124.【SpringBoot 源码刨析C】(八)
124.【SpringBoot 源码刨析C】
20 0
|
7天前
|
监控 druid Java
124.【SpringBoot 源码刨析C】(七)
124.【SpringBoot 源码刨析C】
21 0
|
7天前
|
监控 Java 应用服务中间件
124.【SpringBoot 源码刨析C】(六)
124.【SpringBoot 源码刨析C】
21 0
|
7天前
|
Java API 容器
124.【SpringBoot 源码刨析C】(五)
124.【SpringBoot 源码刨析C】
13 0
相关产品
云迁移中心
推荐文章
更多