之前我们说到 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 将会抛出异常 。
推荐阅读
-
新冠病毒|涉嫌瞒报 商丘疫情有多严重?8月起已有多例本土确诊病例
-
-
雕刻|一块不起眼的翡翠片料,却雕刻出不一般的翡翠作品
-
-
穿搭|莫文蔚这双"亮钻腿"太晃眼睛,这身材的状态,说她30也不为过
-
-
犁花带雨|V6+国六标准,标配空气悬架,买普拉多不如看它,一口气降1047万
-
罗集食|自己做蒜香烤茄子,还去什么烧烤店,有了这秘方
-
郭德纲|郭德纲恩师杨志刚去世!享年83岁,两人曾对峙公堂,郭暂未悼念
-
-
时尚小胖胖|Air Jordan34篮球鞋实战利器!不是吧不是吧你不喜欢?
-
「搜狐新闻」春季气候干燥容易上火,经常吃点它,清热下火不怕长肉
-
锦鲤妈咪|4招轻松解决难题,“糟糕的两岁”有多糟糕?孩子哭闹父母头疼
-
银行|新加坡成立第一家粪便银行:生病时取出移植、比抗生素好
-
豪车世界上工资最高的“司机”,靠月薪就能买下法拉利,还有私人飞机
-
好吃的果实|尾数是几就是你的命运,你甘心任命吗!,生日尾数与命运的关系
-
小众生活意外映衬出水蛇腰,平常人真学不来,街拍:普通T恤扎起来
-
第一财经|连打护盘组合拳,部分精选层企业仍难摘“破发”标签
-
奶球妈咪|生长痛的区分护理不能盲目,孩子总腿疼是在长个子?妈妈别瞎补钙
-
烹饪|这6道炖菜营养滋补,最适合秋天吃,趁着周末学会做给家人补补