之前我们说到 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 将会抛出异常 。
推荐阅读
-
烹饪|在家里自制的麻辣烫,健康又好吃,想吃啥就吃啥
-
-
西藏|我国最“奇怪”的县城,名字听着像骂人,却引众多游客慕名而来
-
团子Seve|左小青身穿黑色深V礼服裙优雅性感,而卫衣搭配百褶裙俏皮可爱!左小青身穿黑色深V礼服裙优雅性感,而卫衣配百褶裙俏皮可爱!
-
旅行|中国人为什么不去柬埔寨旅游?看看他们的风俗、想想就恐怖
-
『』为什么这些外籍华人回国后都极不配合疫情防控工作?
-
浙江:累计完成2200多艘次国际航行船舶、2.7万名船员换班
-
-
重庆小面|我国“最好吃”的5碗面,南方两碗,北方两碗,最后一碗出人意料
-
我的世界|我的世界:之前的水电梯已经OUT了,超快速度水电梯诞生?
-
讲武堂|以军反扑太快:直接两国开炸,伊朗系武装突然炸毁戈兰高地铁丝网
-
太平洋电脑网■AI电话语音系统扩大到英国加拿大和澳大利亚,Duplex
-
-
-
安徽|安徽5所“臭名昭著”的野鸡大学,文凭如同废纸,考生不要被套路
-
曾令旭|CBA-新疆逆转山东夺7连胜 周琦轮休曾令旭伤退
-
松鼠热点|销售火爆!出口转内销商品三天成交金额达1989万元
-
-
八千女鬼上山|哪国的大妈最牛叉?印度、俄罗斯还是中国?
-