之前我们说到 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 将会抛出异常 。
推荐阅读
-
-
金钟奖入围名单|第55届金钟奖入围名单公布,《罪梦者》八提领跑!附入围名单!
-
-
网红家具推荐 网红家具推荐 品质和颜值双在线 让家居设计高端起来
-
「经济观察报」货币市场利率下行空间打开,超额准备金率下调
-
-
-
美图秀秀在线图片处理免费改大小,美图秀秀在线使用如何抠图
-
-
-
-
-
「本田」本田旗舰“大将”正式上市!配2.0T可爆300马力,大五座舒服
-
朱一龙|《微微一笑》竟然藏着这么多男神?认出了白宇,却没认出现男友
-
-
考古盗墓|《史记》记载:修秦始皇陵的人都已被杀,为何司马迁还能描述皇陵内的结构
-
杂志之旅官方|都藏着一颗逞强的心,每一句“没事”的背后
-
中国新闻网|甘肃检方公开聘用书记员线上笔试中止原因:系遭黑客攻击
-
淘宝店铺销量突然下降是什么原因 为什么淘宝销量突然减少
-
中国新闻网|沈跃跃:深入实施巾帼脱贫行动 引导妇女靠勤劳脱贫