之前我们说到 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 将会抛出异常 。
推荐阅读
-
-
-
霸气的小猪佩琦|生死战23分惨案!湖人连赢4场太狠,休城面临重建
-
「久期财经」展望“稳定”,惠誉:确认蓝色光标(300058.SZ)“B+”长期本外币发行人评级
-
|84岁老爷爷参加高考,考前自估630,得知成绩后欲哭无泪
-
-
-
好声音■《好声音》导师阵容确定,没了沈腾贾玲搭档,他能适应吗?
-
利利讲快乐|东方卫视新综艺亮相,放弃《欢乐喜剧人》了吗,90%喜剧大咖聚齐
-
特斯拉|金钱在燃烧 马斯克:特斯拉柏林、德州超级工厂正亏损数十亿美元
-
开屏|杨丽萍到公园偶遇孔雀,孔雀纷纷开屏迎接,万物皆有灵!
-
-
尤文图斯|虽然来得晚些,尤文图斯仍如愿捧杯,C罗续写着他的辉煌
-
睡前常做4种行为,容易诱发失眠,做的越多,失眠的情况越严重
-
演唱会|经典音乐人:开演唱会都不用自己开口的人!全是观众在唱——伍佰
-
-
-
-
-