We’ve gone over the IOC loading process, but now we’re going to take a look at the process, but instead of going into XmlBeanFactory, we’re going to go straight to ApplicationContext, and we’re going to focus on what super(parent) does when it’s initialized.
ClassPathXmlApplicationContext class diagram
Before diving into the source code, be sure to do a good reference point, here we use a class diagram to do a reference.
The code involved in super
There are not many code corresponding to the super method. If we want to follow it, we just need to be careful and understand its hierarchy. There is basically no problem. I’ve got all the source code out here
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(a);this.setParent(parent);
}
public AbstractApplicationContext(a) {
this.resourcePatternResolver = getResourcePatternResolver();
}
protected ResourcePatternResolver getResourcePatternResolver(a) {
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 instanceofConfigurableEnvironment) { getEnvironment().merge((ConfigurableEnvironment) parentEnvironment); }}}Copy the code
From the point of our source, combined with our class diagram, it is not hard to find, actually super (parent) has been in the call of the parent class method, until AbstractApplicationContext, then have the real operation code.
AbstractApplicationContext specific for super (parent) do
Let’s look at this (), this () is our current actual, no arguments constructor in a class initialization is really for us in the end a ClassPathXmlApplicationContext. This point we can follow the initialization code
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) {
returnresource; }}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.
returngetResourceByPath(location); }}}Copy the code
Ultimately, we decide which resource to return based on the beginning of our path
What does setParent(parent) do
setParent(parent); The corresponding code is not much, is relatively straightforward
public void setParent(@Nullable ApplicationContext parent) {
this.parent = parent;
if(parent ! =null) {
Environment parentEnvironment = parent.getEnvironment();
if (parentEnvironment instanceofConfigurableEnvironment) { getEnvironment().merge((ConfigurableEnvironment) parentEnvironment); }}}Copy the code
These pieces of code do one thing: save the parent container and merge the parent’s environment with the current container environment.