In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what is the working principle of super". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the working principle of super"?
ClassPathXmlApplicationContext class diagram
Before delving into the source code, be sure to make a good reference point, here we use the class diagram to do a reference.
The code involved in super
Super this method does not have much code, if we want to follow, we just need to be more careful, understand its hierarchical structure, the basic problem is not big. All the source code has been taken 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 (); 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) }}}
From our source code, combined with our class diagram, it is not difficult to find that super (parent) has been calling the methods of the parent class until AbstractApplicationContext, and then there is no real operation code.
What is specifically done for super (parent) in AbstractApplicationContext
Let's take a look at this (). This () is actually the no-parameter constructor in our current class, and in the end, it initializes a ClassPathXmlApplicationContext for us. We can follow the initialization code at this point.
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);}
In the end, we will decide which resource to return according to the beginning of our path.
What exactly did setParent (parent) do?
SetParent (parent); the corresponding code is not much, and it is relatively simple and clear.
Public void setParent (@ Nullable ApplicationContext parent) {this.parent = parent; if (parent! = null) {Environment parentEnvironment = parent.getEnvironment (); if (parentEnvironment instanceof ConfigurableEnvironment) {getEnvironment () .merge ((ConfigurableEnvironment) parentEnvironment);}
These pieces of code do one thing: save the parent container and merge the environment of the parent container with the current container environment.
Thank you for your reading, the above is the content of "what is the working principle of super". After the study of this article, I believe you have a deeper understanding of what the working principle of super is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.