SpringApplication的run方法代码:
public ConfigurableApplicationContext run(String... args) {
long startTime = System.nanoTime();
DefaultBootstrapContext bootstrapContext = createBootstrapContext();
ConfigurableApplicationContext context = null;
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting(bootstrapContext, this.mainApplicationClass);
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = prepareEnvironment(listeners, bootstrapContext, applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
context.setApplicationStartup(this.applicationStartup);
prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
Duration timeTakenToStartup = Duration.ofNanos(System.nanoTime() - startTime);
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), timeTakenToStartup);
}
listeners.started(context, timeTakenToStartup);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, listeners);
throw new IllegalStateException(ex);
}
try {
Duration timeTakenToReady = Duration.ofNanos(System.nanoTime() - startTime);
listeners.ready(context, timeTakenToReady);
}
catch (Throwable ex) {
handleRunFailure(context, ex, null);
throw new IllegalStateException(ex);
}
return context;
}
其中包含如下代码片段:
printBanner的代码如下:
SpringApplication的bannerMode成员变量默认值为CONSOLE,所以会执行
return bannerPrinter.print(environment, this.mainApplicatonClass, System.out);
print方法代码如下:
调用的getBanner代码如下:
其中getImageBanner和getTextBanner分别从约定好的位置读取图片banner和文本banner,如果我们没有配置,则都为null.最后该方法返回默认的banner,也就是DEFAULT_BANNER,该常量为:
也就是返回一个SpringBootBanner。接下来调用banner.printBanner方法:
SpringBootBanner类的printBanner代码如下:
这个printBanner方法最终拼接打印出我们在控制台看到的效果