ok 这一章,要正式的启动boot,可以看到,bootrun起来后其实是生成了一个ConfigurableApplicationContext,关于ApplicationContext的各种复杂的继承关系后面再单独的讲解。
/**
* Static helper that can be used to run a {@link SpringApplication} from the
* specified source using default settings.
* @param primarySource the primary source to load
* @param args the application arguments (usually passed from a Java main method)
* @return the running {@link ApplicationContext}
*/
public static ConfigurableApplicationContext run(Class<?> primarySource,
String... args) {
return run(new Class<?>[] { primarySource }, args);
}
终于找到了大部队了
/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* @param args the application arguments (usually passed from a Java main method)
* @return a running {@link ApplicationContext}
*/
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
这个方法,就是boot加载的整个流程模板了。我们看看哪里有扩展的地方,
还是一行一行的看。
configureHeadlessProperty();
private void configureHeadlessProperty() {
System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty(
SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless)));
}
这一行是跟awt相关的,网上找了下说明
什么是 java.awt.headless?
Headless模式是系统的一种配置模式。在该模式下,系统缺少了显示设备、键盘或鼠标。
何时使用和headless mode?
Headless模式虽然不是我们愿意见到的,但事实上我们却常常需要在该模式下工作,尤其是服务器端程序开发者。因为服务器(如提供Web服务的主机)往往可能缺少前述设备,但又需要使用他们提供的功能,生成相应的数据,以提供给客户端(如浏览器所在的配有相关的显示设备、键盘和鼠标的主机)。
如何使用和Headless mode?
一般是在程序开始激活headless模式,告诉程序,现在你要工作在Headless mode下,就不要指望硬件帮忙了,你得自力更生,依靠系统的计算能力模拟出这些特性来:
接着
SpringApplicationRunListeners listeners = getRunListeners(args);
这一句,拿到了SpringApplicationRunListeners对象,其实就是封装了
List<SpringApplicationRunListener>的composite对象,当然这些SpringApplicationRunListener也是在meta下面配置好的。然后调用listeners.starting();方法,这些SpringApplicationRunListener会开始做自己的事情。
debug下吧,看拿到了哪些SpringApplicationRunListener,然后每个SpringApplicationRunListener做了什么事情。
如上我加载了一个EventPublishingRunListener
广播了一个ApplicationStartingEvent的消息,告诉系统现在Application准备启动中,那么我们监听这个event在boot启动的时候,hack我们自己的逻辑。
我debug跟进去了下,发现,LoggingApplicationListener监听了这个event,然后log了下相关的信息。如下
,
其实在如下的代码中
可以发现那些listener注册了,如果定义了线程池,就异步执行,如果没有,同步调用,个人建议还是同步吧,发现了问题,即使的shutdown也好发现问题。
接着往下走
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
这一句不重要,略过
ConfigurableEnvironment environment = prepareEnvironment(listeners,applicationArguments);
这一句挺重要的,准备环境,相当于boot的运行时环境,魔鬼藏在细节中,下一章继续的看boot是如何加载配置文件的