之前我们说到 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 将会抛出异常 。
推荐阅读
-
-
-
-
-
华为手机便宜且性能强劲,目前公认最具性价比的3款智能手机,用的人多吗
-
徐帆|抛弃徐帆,放弃许晴,转身娶了兰博基尼总裁,如今12岁儿子帅到掉渣
-
轿车|比亚迪汉都比不过!零跑C01未上市 官方宣称订单已破10万
-
【黑白键奏出的忧伤】轻松让你拥有“漫画腿”,显瘦显腿长!,初夏穿上这3条裤子
-
时尚探究社让她更加的自信迷人,无懈可击之“荧光绿”穿搭
-
育儿佳宝|引千万网友围观,看了都说想生女儿了,日本妈妈晒俩女儿灵魂睡姿
-
杨幂 |杨幂即将开启霸屏模式,一口气官宣4部新剧,男主个个是当红男神
-
企业在碰到案子时,发现以前一份合同要追讨货款过了诉讼时效,是不是能追究企业所聘请的常年法律顾问的责任
-
【XXP数码捣鼓屋】Pro,拒绝买前生产力,买后爱奇艺,利用扩展坞增加实用性,新iPad
-
太平洋电脑网▲Bar开启公测:让你轻松了解时下新闻热点,微软News
-
-
微信朋友圈可以发20张图真的假的,微信朋友圈怎么发20张图2022
-
-
手机教授|很实用,朋友圈又升级了,微信悄悄更新的新功能
-
[金融文化]【理论前沿】优化营业网点服务的几点思考
-
钟馗|皮肤碎片首次升值,千年之狐别错过,6元四选一,1级限定是辅助?