SpringBoot banner

8 篇文章 0 订阅
2 篇文章 0 订阅

Spring Boot 打印banner当应用启动时,可以显示应用图标,版本,名称等相关信息,可以通过配置文件指定banner打印模式:

spring:
  main:
    banner-mode: OFF,CONSOLE,LOG

具体流程如下:

  1. SpringApplication.run(String… args) 执行打印banner入口,环境变量加载完成后首先打印就是banner。
SpringApplication:
public ConfigurableApplicationContext run(String... args) {
    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    ConfigurableApplicationContext context = null;
    Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
    this.configureHeadlessProperty();
    SpringApplicationRunListeners listeners = this.getRunListeners(args);
    listeners.starting();

    Collection exceptionReporters;
    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
        this.configureIgnoreBeanInfo(environment);
        Banner printedBanner = this.printBanner(environment);
        context = this.createApplicationContext();
        exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
        this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        this.refreshContext(context);
        this.afterRefresh(context, applicationArguments);
        stopWatch.stop();
        if (this.logStartupInfo) {
            (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
        }

        listeners.started(context);
        this.callRunners(context, applicationArguments);
    } catch (Throwable var10) {
        this.handleRunFailure(context, var10, exceptionReporters, listeners);
        throw new IllegalStateException(var10);
    }

    try {
        listeners.running(context);
        return context;
    } catch (Throwable var9) {
        this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
        throw new IllegalStateException(var9);
    }
}
  1. Run方法将调用printBanner(ConfigurableEnvironment environment),可以通过不同的环境来打印不同的banner, 如果没有指定Banner, DefaultResourceLoader做为默认banner实现类来打印banner。
SpringApplication:
private Banner printBanner(ConfigurableEnvironment environment) {
    if (this.bannerMode == Mode.OFF) {
        return null;
    } else {
        ResourceLoader resourceLoader = this.resourceLoader != null ? this.resourceLoader : new DefaultResourceLoader((ClassLoader)null);
        SpringApplicationBannerPrinter bannerPrinter = new SpringApplicationBannerPrinter((ResourceLoader)resourceLoader, this.banner);
        return this.bannerMode == Mode.LOG ? bannerPrinter.print(environment, this.mainApplicationClass, logger) : bannerPrinter.print(environment, this.mainApplicationClass, System.out);
    }
}
  1. SpringApplicationBannerPrinter作为主控制类,封装了banner的业务操作和调和各个banner之前协调工作
  • ResourceLoader 用来载入banner中的图片和文本文件;
  • fallbackBanner指定默认banner,如果找不到其他banner,则使用此fallbackBanner作为默认banner.
class SpringApplicationBannerPrinter {
    static final String BANNER_LOCATION_PROPERTY = "spring.banner.location";
    static final String BANNER_IMAGE_LOCATION_PROPERTY = "spring.banner.image.location";
    static final String DEFAULT_BANNER_LOCATION = "banner.txt";
    static final String[] IMAGE_EXTENSION = new String[]{"gif", "jpg", "png"};
    private static final Banner DEFAULT_BANNER = new SpringBootBanner();
    private final ResourceLoader resourceLoader;
    private final Banner fallbackBanner;

    SpringApplicationBannerPrinter(ResourceLoader resourceLoader, Banner fallbackBanner) {
        this.resourceLoader = resourceLoader;
        this.fallbackBanner = fallbackBanner;
    }
。。。。。
}
  1. Banner作为打印接口,此接口有三个实现子类,分别是SpringBootBanner,ImageBanner,ResourceBanner,打印banner有三种Mode
    Mode :
  • OFF 禁用banner;
  • CONSOLE 控制台打印banner;
  • LOG 日志里打印banner。
@FunctionalInterface
public interface Banner {

   /**
    * Print the banner to the specified print stream.
    * @param environment the spring environment
    * @param sourceClass the source class for the application
    * @param out the output print stream
    */
   void printBanner(Environment environment, Class<?> sourceClass, PrintStream out);

   /**
    * An enumeration of possible values for configuring the Banner.
    */
   enum Mode {

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

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

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

   }

}
  1. SpringBootBanner单独使用,只有在没有指定ImageBanner和ResourceBanner时,default才会被调用.
  2. ResourceBanner 指定一段文本文件作为banner text 部分输出,文本中可以设置表达式来获得配置文件一些信息输出到banner内容中,也可以用配置文件来指定banner文本文件位置和字符编码
application.yaml:
spring:
  banner:
    location: banner-str.txt
    charset: UTF-8
---------------------------------
banner-str.txt
spring boot: ${spring-boot.version}
application: ${spring.app.name}, ${spring.app.version}

在这里插入图片描述

  1. ImageBanner 指定一个文件作为banner的图标,此banner将在ResourceBanner 之前调用,支持图片格式:gif", “jpg”, “png”,可以通过配置文件指定banner的图片位置,或设置打印图片相关属性,ImageBanner将引用ansi包将图片会自动转换成 ASCII art 的形式进行输出。
application.yaml
spring:
  main:
    banner-mode: CONSOLE
  title: my app
  app:
    name:  test
    version: v1.0.0
  banner:
    location: banner-str.txt
    image:
      location: banner-img.jpg
      width: 150
      height: 0
      margin: 0
      invert: false
      bitdepth: 4
      pixelmode: BLOCK

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值