springApplication启动
前情回顾
在上一篇中我们看了springApplication实例的初始化过程,在实例化的时候,先是设置sources来源位置,然后根据classpath设置了应用类型,然后添加了写初始化器和监听器,最后设置了他的启动类是哪个。接下来我们看看SpringApplication的启动过程,下面一定比初始化过程复杂,慢慢看,看多少算多少,fighting~~~~~~~~~~
springApplication.run(args)
上面初始化感觉都好多过程,下面run肯定更复杂,加油,坚持!!!
/**
* Run the Spring application, creating and refreshing a new
* {@link ApplicationContext}.
* 运行spring应用,创建和刷新一个新的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;
}
大概画了下springApplication启动过程。比较复杂,为了结构稍微好看点,分了多篇篇幅来写,比较简单的地方就没有另开篇幅。
StopWatch
StopWatch,懒得自己翻译了,直接看工具翻译的吧:
简单点就是说StopWatch就是一个计时器,get一个新技能。
demo一下,发现hutool里面也有StopWatch,哈哈:
configureHeadlessProperty
这里我真是没看懂在干嘛,这貌似涉及到jdk的知识了,贴个代码先撤吧。
private void configureHeadlessProperty() { System.setProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, System.getProperty(SYSTEM_PROPERTY_JAVA_AWT_HEADLESS, Boolean.toString(this.headless))); }
SpringApplicationRunListeners
SpringApplicationRunListeners listeners = getRunListeners(args);
private SpringApplicationRunListeners getRunListeners(String[] args) {
Class<?>[] types = new Class<?>[] { SpringApplication.class, String[].class };
return new SpringApplicationRunListeners(logger,
getSpringFactoriesInstances(SpringApplicationRunListener.class, types, this, args));
}
返回一个SpringApplicationRunListeners,我们上回看过getSpringFactoriesInstances是在实例化工厂对象,这里就先不深究了,debug看下返回了个啥:
SpringApplicationRunListeners的实例,里面包含一个LogAdapter实例和一个EventPublishingRunListener,log实例就先不管了,反正就是拿来写日志的,先看看EventPublishingRunListener吧:
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
获取到监听器后立马给启动了,看下启动过程:
void starting() {
for (SpringApplicationRunListener listener : this.listeners) {
listener.starting();
}
}
@Override
public void starting() {
this.initialMulticaster.multicastEvent(new ApplicationStartingEvent(this.application, this.args));
}
启动的时候发布了一个ApplicationStartingEvent事件,看下这个事件的监听则有哪些:
这些侦听者在ApplicationStartingEvent触发时干了什么?详见:跟我一起阅读SpringBoot源码(十)——侦听器
ApplicationArguments
这里是初始化的一个默认参数,我们看看默认参数有啥:
public DefaultApplicationArguments(String... args) {
Assert.notNull(args, "Args must not be null");
this.source = new Source(args);
this.args = args;
}
通过main方法输入的args来生成一个Source,然后把参数存起来,我们一般输入的args是{},那生成的默认参数是怎样的呢?debug下:
ConfigurableEnvironment
跟我一起阅读SpringBoot源码(三)——ConfigurableEnvironment
configureIgnoreBeanInfo(environment);
在准备好环境后,执行了一个配置操作:
private void configureIgnoreBeanInfo(ConfigurableEnvironment environment) {
if (System.getProperty(CachedIntrospectionResults.IGNORE_BEANINFO_PROPERTY_NAME) == null) {
Boolean ignore = environment.getProperty("spring.beaninfo.ignore", Boolean.class, Boolean.TRUE);
System.setProperty(CachedIntrospectionResults.IGNORE_BEANINFO_PROPERTY_NAME, ignore.toString());
}
}
意思就是先看系统环境里面有没有配置"spring.beaninfo.ignore"
属性,如果有,就拿环境中配置的属性来替换。但是并没有下一步操作,我想是对环境准备的一个补充吧。后面如果需要应该会用到这个属性。当然,现在这个demo里面没有用到这个属性。
Banner
ConfigurableApplicationContext
SpringBootExceptionReporter
上下文创建后,根据上下文有实例化了一个SpringBoot异常报告对象:
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
这里就不进去看了,应该是获取了一些异常报告对象。
准备上下文
跟我一起阅读SpringBoot源码(八)——准备应用上下文
刷新上下文
上下文刷新后处理
【啃不动了,今天就到这里吧,先歇下,未完待续……】