您好,登录后才能下订单哦!
这篇文章主要讲解了“super的工作原理是什么”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“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,然后才有真正的操作代码。
先来看看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);对应的代码并不多,也是比较简单明了的
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的工作原理是什么”的内容了,经过本文的学习后,相信大家对super的工作原理是什么这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。