Spring Boot 打印banner当应用启动时,可以显示应用图标,版本,名称等相关信息,可以通过配置文件指定banner打印模式:
spring: main: banner-mode: OFF,CONSOLE,LOG具体流程如下:
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); } } 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); } } 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; } 。。。。。 } 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 } } SpringBootBanner单独使用,只有在没有指定ImageBanner和ResourceBanner时,default才会被调用.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} 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