Spring源码5. printBanner()打印Banner

上篇回顾

上一篇prepareEnvironment()准备环境主要分析了StandardServletEnvironment的创建, 以及系统属性, 环境变量, 配置文件等属性文件的加载顺序


目录

1. 打印Banner printBanner()
2. SpringApplicationBannerPrinter
3. 总结

1. 打印Banner printBanner()

  1. 实例化ResourceLoader, 默认为null, 所以实例化一个DefaultResourceLoader
  2. 实例化一个SpringApplicationBannerPrinter对象
  3. 调用SpringApplicationBannerPrinter的print方法
public class SpringApplication {
    public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection exceptionReporters = new ArrayList<>();
        configureHeadlessProperty();
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                    args);
            ConfigurableEnvironment environment = prepareEnvironment(listeners,
                    applicationArguments);
            configureIgnoreBeanInfo(environment);
            //本文的重点
            //打印Banner
            Banner printedBanner = printBanner(environment);
        //....
    }
    
    //打印Banner
    private Banner printBanner(ConfigurableEnvironment environment) {
        //如果Mode为OFF
        //那么不打印Banner
        if (this.bannerMode == Banner.Mode.OFF) {
            return null;
        }
        
        //ResourceLoader资源加载类
        //默认为DefaultResourceLoader
        ResourceLoader resourceLoader = (this.resourceLoader != null)
                ? this.resourceLoader : new DefaultResourceLoader(getClassLoader());
        //实例化一个SpringApplicationBannerPrinter
        SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter(
                resourceLoader, this.banner);
        
        //调用SpringApplicationBannerPrinter#print方法打印Banner
        if (this.bannerMode == Mode.LOG) {
            //日志打印Banner
            return bannerPrinter.print(environment, this.mainApplicationClass, logger);
        }
        //控制台打印Banner
        return bannerPrinter.print(environment, this.mainApplicationClass, System.out);
    }
}

2. SpringApplicationBannerPrinter

SpringApplicationBannerPrinter打印banner过程分为5步

  1. 加载图片Banner
    • 先加载spring.banner.image.location指定的图片Banner
    • 不存在的话, 分别取加载classpath下banner.gif, banner.jpg和banner.png
    • 实例化为ImageBanner, 维护到Banners对象中
  2. 加载文本Banner
    • 先加载spring.banner.location指定的文本banner
    • 不存在的话, 加载classpath下的banner.txt文本
    • 实例化一个ResourceBanner, 维护到Banners对象中
  3. 图片和文本Banner都不存在的话, 如果通过构造函数, 传入了fallbackBanner, 那么返回fallbackBanner
  4. 图片, 文本和fallbackBanner都不存在, 那么返回默认的SpringBootBanner
  5. 打印Banner, 返回一个PrintedBanner
    • 如果Banners列表中同时存在图片和文本Banner,那么都会打印出来
class SpringApplicationBannerPrinter {

    //文本banner位置
    static final String BANNER_LOCATION_PROPERTY = "spring.banner.location";

    //图片banner位置
    static final String BANNER_IMAGE_LOCATION_PROPERTY = "spring.banner.image.location";

    //默认banner名称
    static final String DEFAULT_BANNER_LOCATION = "banner.txt";
    
    //支持的图片扩展名
    static final String[] IMAGE_EXTENSION = { "gif", "jpg", "png" };

    //默认banner类
    private static final Banner DEFAULT_BANNER = new SpringBootBanner();

    //日志打印Banner
    public Banner print(Environment environment, Class sourceClass, Log logger) {
        //获取banner
        Banner banner = getBanner(environment);
        try {
            //打印Banner
            logger.info(createStringFromBanner(banner, environment, sourceClass));
        }
        catch (UnsupportedEncodingException ex) {
            logger.warn("Failed to create String for banner", ex);
        }
        return new PrintedBanner(banner, sourceClass);
    }

    //控制台打印Banner
    public Banner print(Environment environment, Class sourceClass, PrintStream out) {
        //获取到Banners对象, 其中维护了Banner列表
        //如果Banners中没有对象, 那么判断fallbackBanner
        //如果都没有的话,返回默认的SpringBootBanner
        Banner banner = getBanner(environment);
        //一次打印Banner
        banner.printBanner(environment, sourceClass, out);
        return new PrintedBanner(banner, sourceClass);
    }
    
    //获取Banner
    //Banners类保存多个banner列表
    private Banner getBanner(Environment environment) {
        //获取Banners列表
        Banners banners = new Banners();
        //添加ImageBanner
        banners.addIfNotNull(getImageBanner(environment));
        //添加TextBanner
        banners.addIfNotNull(getTextBanner(environment));
        if (banners.hasAtLeastOneBanner()) {
            //如果有自定义Banner,则立即返回
            return banners;
        }
        if (this.fallbackBanner != null) {
            //如果构造函数传参过来
            //也就是通过java自定义了fallbackBanner
            //那么返回fallbackBanner
            return this.fallbackBanner;
        }
        //如果没有的话, 返回默认Banner, SpringBootBanner
        return DEFAULT_BANNER;
    }

    //获取文本banner
    private Banner getTextBanner(Environment environment) {
        //优先获取spring.banner.location指定的banner位置
        //然后查找classpath下的banner.txt
        String location = environment.getProperty(BANNER_LOCATION_PROPERTY,
                DEFAULT_BANNER_LOCATION);
        Resource resource = this.resourceLoader.getResource(location);
        if (resource.exists()) {
            //存在的话,立即返回ResourceBanner
            return new ResourceBanner(resource);
        }
        //都没有的话,返回null
        return null;
    }

    //获取图片banner
    private Banner getImageBanner(Environment environment) {
        //优先获取spring.banner.image.location指定的图片banner
        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;
        }
        for (String ext : IMAGE_EXTENSION) {
            //如果没有指定spring.banner.image.location
            //那么查找classpath下的banner.jpg, banner.png, banner.gif
            Resource resource = this.resourceLoader.getResource("banner." + ext);
            if (resource.exists()) {
                //有一个存在的话,立即返回该Banner
                return new ImageBanner(resource);
            }
        }
        //都不存在的话返回null
        return null;
    }
}

3. 总结

打印Banner执行步骤如下:

  1. 首先加载图片Banner
  2. 然后加载文本Banner
  3. 再判断fallbackBanner
  4. 最后返回默认的SpringBootBanner
  5. 打印Banner, 返回一个PrintedBanner对象

下一篇

我们将会在下一篇createApplicationContext()创建应用上下文, 继续阅读springboot源码

你可能感兴趣的:(Spring源码5. printBanner()打印Banner)