前文说过了prepareEnvironment()方法
接下来我们说createApplicationContext()方法
在createApplicationContext方法之前还有一步:
run(){
...
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
context = this.createApplicationContext();
...
}
Banner printedBanner = this.printBanner(environment);
该方法是作用是设置spring.beaninfo.ignore属性以及BAANER图,配置的spring.beaninfo.ignore添加到名叫systemProperties的PropertySource中,默认为true即开启没什么好说的,然后就是打印的那些设置输出打印的类以及属性。
banner图:
好的,我们直接进入createApplicationContext
public static final String DEFAULT_SERVLET_WEB_CONTEXT_CLASS = "org.springframework.boot."
+ "web.servlet.context.AnnotationConfigServletWebServerApplicationContext";
public static final String DEFAULT_REACTIVE_WEB_CONTEXT_CLASS = "org.springframework."
+ "boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext";
public static final String DEFAULT_CONTEXT_CLASS = "org.springframework.context."
+ "annotation.AnnotationConfigApplicationContext";
protected ConfigurableApplicationContext createApplicationContext() {
Class<?> contextClass = this.applicationContextClass;
//判断当前applicationContextClass是否为空,为空则根据web应用类型创建上下文
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);
}
}
//最终转换成是ConfigurableApplicationContext
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}
根据应用类型创建对应的ApplicationContext对象,这里最终创建的是AnnotationConfigServletWebServerApplicationContext。
要了解AnnotationConfigServletWebServerApplicationContext的实例化过程中都做了什么,先要了解其继承结构。下面是其类图。
从类图中我们可知,类结构比较深,我们从上往下来看各个父类的构造方法(实现的接口先不看),看看构造方法体里面所做的事。
DefaultResourceLoader:默认资源加载器,获取默认的类加载器,获取的是当前线程的上下文类加载器。
AbstractApplicationContext:抽象应用上下文,初始化属性包括:
public AbstractApplicationContext() {
this.logger = LogFactory.getLog(this.getClass());
this.id = ObjectUtils.identityToString(this);
this.displayName = ObjectUtils.identityToString(this);
this.beanFactoryPostProcessors = new ArrayList();
this.active = false;
this.closed = false;
this.activeMonitor = new Object();
this.startupShutdownMonitor = new Object();
this.applicationListeners = new LinkedHashSet();
this.resourcePatternResolver = this.getResourcePatternResolver();
}
1、会得到Log对象
2、为上下文设置唯一id和显示名称
3、初始化BeanFactoryPostProcessors 的List集合
4、是否活跃标识active,1为true,0为false
5、是否已关闭标识closed,1为true,0为false
6、刷新和销毁的同步对象startupShutdownMonitor
7、初始化ApplicationListener<?>的Set集合
8、在构造方法中实例化一个resourcePatternResolver ,参数就是PathMatchingResourcePatternResolver
protected ResourcePatternResolver getResourcePatternResolver() {
return new PathMatchingResourcePatternResolver(this);
}
public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
this.resourceLoader = resourceLoader;
}
PathMatchingResourcePatternResolver是一个Ant模式通配符的Resource查找器,可以用来查找类路径下或者文件系统中的资源。在其中会实例化一个AntPathMatcher,该类实现Ant风格的路径模式
resourcePatternResolver:也就是资源模式解析器;实际类型是PathMatchingResourcePatternResolver,它是基于模式匹配的,默认使用AntPathMatcher进行路径匹配,它除了支持ResourceLoader支持的前缀外,还额外支持“classpath*:”用于加载所有匹配的类路径Resource。此外beanFactoryPostProcessors属性此时已经初始化了。
GenericApplicationContext,通用应用上下文:
然后在GenericApplicationContext中同样会做一些初始化,包括:
public GenericApplicationContext() {
this<