持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第4天,点击查看活动详情
ApplicationContext
前面都很多地方提到ApplicationContext,那究竟什么是ApplicationContext呢?
Context:语境、情况;ApplicationContext:应用环境; 其实ApplicationContext就是我们通常听说的应用运行的上下文。 至于什么是上下文,作用是什么,还是直接看下SpringApplication的注释是怎么说的吧。
我们接着看下这里的ConfigurableApplicationContext,其实从名称我们能大概猜到他其实是集成ApplicationContext的是关于配置方面的上下文。不过我们还是看下注释文档怎么说的。
ConfigurableApplicationContext
这个类太复杂了,就先不细讲了。
createApplicationContext
创建应用上下文,好像是干货,进去看下:
/**
*用于创建ApplicationContext策略方法。 默认情况下,此方法将使用任何明确设置的应用程序上下文或应用程序上下文类
*,然后再使用合适的默认值。
* @return the application context (not yet refreshed)
* @see #setApplicationContextClass(Class)
*/
protected ConfigurableApplicationContext createApplicationContext() {
//如果没记错,这里应该是null
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
switch (this.webApplicationType) {
case SERVLET:
contextClass = Class.forName(DEFAULT_SERVLET_WEB_CONTEXT_CLASS);
break;
case REACTIVE:
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
break;
default:
contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, please specify an ApplicationContextClass", ex);
}
}
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
这里又是根据应用类型来的,这里就不深究了,看下当前的执行结果吧。
现在的上下文是一个基于AnnotationConfigServletWebServerApplicationContext的上下文???那看下AnnotationConfigServletWebServerApplicationContext这个又是啥嘛。
AnnotationConfigServletWebServerApplicationContext
可以看到AnnotationConfigServletWebServerApplicationContext只是ConfigurableApplicationContext的29(当前环境)个实现之一:
大意应该就是这个上下文支持@Configuration、@Component和复合JSR-330的类作为输入吧。先不深究。反正就创建了一个一个上下文环境吧。