之前我们粗略的过了一遍IOC加载流程,现在我们开始细扒一下这个流程,不过我们不再去讲XmlBeanFactory,这里直接讲ApplicationContext,本文主要聊聊初始化的时候,super(parent)到底做了什么。
ClassPathXmlApplicationContext类图
深扒源码之前,一定要做好参考点,这里我们使用类图来做一个参考。
super涉及的代码
super这个方法对应的代码并不多,如果我们要去跟的话,只需要仔细一点,了解他的层级结构,基本问题不大。这里把所有源码都拿出来了
public ClassPathXmlApplicationContext(ApplicationContext parent) {
super(parent);
}
public AbstractXmlApplicationContext(@Nullable ApplicationContext parent) {
super(parent);
}
public AbstractRefreshableConfigApplicationContext(@Nullable ApplicationContext parent) {
super(parent);
}
public AbstractRefreshableApplicationContext(@Nullable ApplicationContext parent) {
super(parent);
}
public AbstractApplicationContext(@Nullable ApplicationContext parent) {
this();
this.setParent(parent);
}
public AbstractApplicationContext() {
this.resourcePatternResolver = getResourcePatternResolver();
}
protected ResourcePatternResolver getResourcePatternResolver() {
return new PathMatchingResourcePatternResolver(this);
}
public PathMatchingResourcePatternResolver(ResourceLoader resourceLoader) {
Assert.notNull(resourceLoader, "ResourceLoader must not be null");
this.resourceLoader = resourceLoader;
}
public void setParent(@Nullable ApplicationContext parent) {
this.parent = parent;
if (parent != null) {
Environment parentEnvironment = parent.getEnvironment();
if (parentEnvironment instanceof ConfigurableEnvironment) {
getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
}
}
}
从我们的源码上来看,结合我们的类图,不难发现,其实super(parent)一直在调用父类的方法,直到AbstractApplicationContext,然后才有真正的操作代码。
AbstractApplicationContext中具体给super(parent)做了什么
先来看看this(),this()实际就是我们当前类里面的无参构造方法,最终其实也是给我们初始化了一个ClassPathXmlApplicationContext。这一点我们可以跟一下初始化的代码
public Resource getResource(String location) {
return getResourceLoader().getResource(location);
}
public Resource getResource(String location) {
Assert.notNull(location, "Location must not be null");
for (ProtocolResolver protocolResolver : getProtocolResolvers()) {
Resource resource = protocolResolver.resolve(location, this);
if (resource != null) {
return resource;
}
}
if (location.startsWith("/")) {
return getResourceByPath(location);
}
else if (location.startsWith(CLASSPATH_URL_PREFIX)) {
return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());
}
else {
try {
// Try to parse the location as a URL...
URL url = new URL(location);
return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));
}
catch (MalformedURLException ex) {
// No URL -> resolve as resource path.
return getResourceByPath(location);
}
}
}
最终是按照我们的路径的开头判断返回哪种resource
setParent(parent)到底做了些啥
setParent(parent);对应的代码并不多,也是比较简单明了的
public void setParent(@Nullable ApplicationContext parent) {
this.parent = parent;
if (parent != null) {
Environment parentEnvironment = parent.getEnvironment();
if (parentEnvironment instanceof ConfigurableEnvironment) {
getEnvironment().merge((ConfigurableEnvironment) parentEnvironment);
}
}
}
这几部代码就做了一件事情:保存父容器,并将父容器的环境与当前容器环境合并。