之前我们说到 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 将会抛出异常 。
推荐阅读
-
描写秋天的诗句这样的诗句我还知道哪些?秋天的诗句词语优美句子
-
-
网媒大咖杨文彬|美媒:白宫将要求民众忍受新冠病毒,美国累计确诊超283万例
-
吴蔚升杀球集锦。马来西亚羽毛球运动员吴蔚升的球拍磅数是多少
-
大S|大S婚内出轨再增新料!曾单方面宣布离婚,男方开心晒纹身表白
-
-
周星驰|周星驰一生代言两个广告,一个众人皆知,一个哭笑不得
-
-
情怀历史号|另一个为曹魏效力!,三国时有两个董和:一个辅佐刘备
-
Mac 下memcached 的安装与使用简单分享
-
吞了仙女粉|路上遭遇抢劫,劫匪打开行囊后,跪送他离开,民国官员辞官回乡
-
九游网|《DNF》10月15日礼物大派送活动怎么样 礼物大派送活动推荐
-
证券日报|多家券商上半年新增开户数超30万 高净值客户群成挖掘重点
-
-
-
松浦正敬|日本一高中88人集体感染 市长:不要找“毒王” 谁都有可能确诊
-
史海观复|择偶标准非有钱人不深交,韩国“塑料美人”潘南奎:诈骗前男友10亿
-
就是这个中国人,拒绝诺贝尔奖提名,逝世后,抬棺人身份不一般!
-
潮人集|被洋人老公宠成宝,生下混血儿子太惊艳,曾经的“最丑国模”吕燕
-