之前我们说到 doRegisterBeanDefinitions 方法会导致递归 , 在该方法的最后一行得到了验证 。 如果里面定义了类型的标签的话(嵌套 beans)
这里说明一下 , 早在我们介绍 loadBeanDefinitions 方法中 , Spring 利用了一个 Set 集合来探测是否存在循环的 import 导入配置文件 , 如果出现了循环的 import 导入 , Spring 会在 loadBeanDefinitions 中抛出异常 。 这种出现必然是有原因的 , 我们跟到 importBeanDefinitionResource 中看看 Spring 是如何处理 import 这种标签的 。
protected void importBeanDefinitionResource(Element ele) {// 获取元素中的 resource 属性String location = ele.getAttribute(RESOURCE_ATTRIBUTE);if (!StringUtils.hasText(location)) {getReaderContext().error("Resource location must not be empty", ele);return;}// Resolve system properties: e.g. "${user.dir}"location = getReaderContext().getEnvironment().resolveRequiredPlaceholders(location);Set actualResources = new LinkedHashSet<>(4);// Discover whether the location is an absolute or relative URIboolean absoluteLocation = false;try {// 判断 resource 的值是否为绝对路径absoluteLocation = ResourcePatternUtils.isUrl(location) || ResourceUtils.toURI(location).isAbsolute();}catch (URISyntaxException ex) {// cannot convert to an URI, considering the location relative// unless it is the well-known Spring prefix "classpath*:"}// Absolute or relative?if (absoluteLocation) { //绝对路径try {// 调用了 loadBeanDefinitions 方法int importCount = getReaderContext().getReader().loadBeanDefinitions(location, actualResources);if (logger.isTraceEnabled()) {logger.trace("Imported " + importCount + " bean definitions from URL location [" + location + "]");}}catch (BeanDefinitionStoreException ex) {getReaderContext().error("Failed to import bean definitions from URL location [" + location + "]", ele, ex);}}else { // 相对路径// No URL -> considering resource location as relative to the current file.try {int importCount;Resource relativeResource = getReaderContext().getResource().createRelative(location);if (relativeResource.exists()) {// 调用了 loadBeanDefinitions 方法importCount = getReaderContext().getReader().loadBeanDefinitions(relativeResource);actualResources.add(relativeResource);}else {String baseLocation = getReaderContext().getResource().getURL().toString();// 调用了 loadBeanDefinitions 方法importCount = getReaderContext().getReader().loadBeanDefinitions(StringUtils.applyRelativePath(baseLocation, location), actualResources);}}catch (IOException ex) {getReaderContext().error("Failed to resolve current resource location", ele, ex);}catch (BeanDefinitionStoreException ex) {getReaderContext().error("Failed to import bean definitions from relative location [" + location + "]", ele, ex);}}Resource[] actResArray = actualResources.toArray(new Resource[0]);getReaderContext().fireImportProcessed(location, actResArray, extractSource(ele));}复制代码不管 import 标签的 resource 属性配置的是绝对路径还是相对路径 , 我们在代码中不难发现 , 两个分支中都调用了 loadBeanDefinitions 这个方法 。 这都会导致 Spring 在解析 import 标签的同时去判断是否 import 循环的 xml 文件引用 , 也从侧面验证了如果循环 import 了 , Spring 将会抛出异常 。
推荐阅读
-
-
-
生肖|9月,再续前缘,旧情人心软了,3生肖再次收获挚爱,纠纠缠缠
-
「互联网圈内事官方」中兴通讯独立董事被曝涉性侵养女!当事人:我这辈子都不会忘!
-
莫红论育儿|朱茵发福明显让人认不出,被网友调侃是“大妈”,港普4F同框
-
-
电池更换|总投资超20亿元 临港五年内最多新增5000台换电重卡
-
青年|王者荣耀:花木兰峡谷严厉对待苏烈,凯,但私底下是很温柔哟
-
|47岁佘诗曼扎公主头现身跟平时披发差距大,我感觉她一下就变老了
-
-
人民日报微信公号|女童穿越马路,轿车疾驶而来,被撞飞的却是他
-
好影天天荐|郭麒麟低调回应,郭麒麟“翻车”了?9天前上节目被知名导演炮轰
-
-
据说,今年农村这6大补贴很心动哦,不来看下你家能领取几种
-
穿搭|穿靴子的季节到了,美女用马丁靴搭配工装连体裤,展现酷飒之美
-
作文:当小学生的作文“翻车”,家长表情收不住,老师:标点引起大误会
-
-
【球长社圈】骚气!丹尼-格林社交媒体晒出自己新发型视频
-
帅国|服务信托成增长“新动力”,高压下的信托业找到突破口了?
-
最新热文|向佐真的别当演员了,凭一己之力拉低整部剧颜值