热门标签 | HotTags
当前位置:  开发笔记 > 前端 > 正文

spring是如何解析xml配置文件中的占位符

这篇文章主要介绍了spring是如何解析xml配置文件中的占位符,帮助大家更好的理解和使用spring框架,感兴趣的朋友可以了解下

前言

我们在配置Spring Xml配置文件的时候,可以在文件路径字符串中加入 ${} 占位符,Spring会自动帮我们解析占位符,这么神奇的操作Spring是怎么帮我们完成的呢?这篇文章我们就来一步步揭秘。

1.示例

ClassPathXmlApplicationContext applicatiOnContext= new ClassPathXmlApplicationContext();
applicationContext.setConfigLocation("${java.version}.xml");
applicationContext.refresh();
String[] beanNames = applicationContext.getBeanDefinitionNames();
for (String beanName : beanNames) {
 System.out.println(beanName);
}

这段代码在我工程里是会报错的,如下:

Caused by: java.io.FileNotFoundException: class path resource [1.8.0_144.xml] cannot be opened because it does not exist
 at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:190)
 at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
 ... 11 more

可以看到报错里面的文件路径变成了1.8.0_144.xml,也就是说Spring帮我们把${java.version}解析成了实际值。

2.原理

AbstractRefreshableConfigApplicationContext
我们在之前的文章里提到过这个类的resolve方法,我们再来瞧一眼:

/**
  * Resolve the given path, replacing placeholders with corresponding
  * environment property values if necessary. Applied to config locations.
  * @param path the original file path
  * @return the resolved file path
  * @see org.springframework.core.env.Environment#resolveRequiredPlaceholders(String)
  */
 protected String resolvePath(String path) {
  //通过当前环境去 解析 必要的占位符
  return getEnvironment().resolveRequiredPlaceholders(path);
 }

获取当前环境,这个环境在示例代码中就是 StandardEnvironment ,并且根据当前环境去解析占位符,这个占位符解析不到还会报错。

resolveRequiredPlaceHolders由StandardEnvironment的父类AbstractEnvironment实现。

AbstractEnvironment

//把propertySources放入 Resolver中
private final ConfigurablePropertyResolver propertyResolver =
   new PropertySourcesPropertyResolver(this.propertySources);
@Override
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
 return this.propertyResolver.resolveRequiredPlaceholders(text);
}

这里的propertySources很重要了,从命名也可以看出我们解析占位符的来源就是从这个集合中来的。这个集合是在我们StandardEnvironment实例化的时候去自定义的。

StandardEnvironment

/**
  * Create a new {@code Environment} instance, calling back to
  * {@link #customizePropertySources(MutablePropertySources)} during construction to
  * allow subclasses to contribute or manipulate(操作) {@link PropertySource} instances as
  * appropriate.
  * @see #customizePropertySources(MutablePropertySources)
  */
 //StandardEnvironment 实例化调用
 public AbstractEnvironment() {
  customizePropertySources(this.propertySources);
 }
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {

 //todo Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,
 //todo getenv方法返回的变量大多于系统相关,
 //todo getProperty方法返回的变量大多与java程序有关。
 //https://www.cnblogs.com/Baronboy/p/6030443.html
 propertySources.addLast(new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));

 //SystemEnvironmentPropertySource 是System.getenv()
 propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}

最重要的肯定是我们的 propertyResolver.resolveRequiredPlaceholders 方法了,propertyResolver.resolveRequiredPlaceholders其实是PropertySourcesPropertyResolver的父类AbstractPropertyResolver来实现。

AbstractPropertyResolver

//创建一个占位符的helper去解析
@Override
public String resolveRequiredPlaceholders(String text) throws IllegalArgumentException {
 if (this.strictHelper == null) {
  //不忽略
  this.strictHelper = createPlaceholderHelper(false);
 }
 return doResolvePlaceholders(text, this.strictHelper);
}
 //私有方法
 //是否忽略 无法解决的占位符
private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolvablePlaceholders) {

  //默认使用${ placeholderPrefix
  return new PropertyPlaceholderHelper(this.placeholderPrefix, this.placeholderSuffix,
    this.valueSeparator, ignoreUnresolvablePlaceholders);
}
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {

  //PlaceholderResolver function interface
  //todo important 重要的是这个getPropertyAsRawString
  return helper.replacePlaceholders(text, this::getPropertyAsRawString);
 }

这里的 this::getPropertyAsRawString 很重要,利用了java8的函数式接口来实现。它的定义在AbstractPropertyResolver里

/**
  * Retrieve the specified property as a raw String,
  * i.e. without resolution of nested placeholders.
  * @param key the property name to resolve
  * @return the property value or {@code null} if none found
  */
 @Nullable
 protected abstract String getPropertyAsRawString(String key);

但是我们在doResolvePlaceholders里指向的this,所以还得看PropertySourcesPropertyResolver类。

PropertySourcesPropertyResolver

//提供给函数接口 PlaceholderResolver
 //todo 解析 xml配置文件路径占位符的时候调用的是这个 2020-09-11
 @Override
 @Nullable
 protected String getPropertyAsRawString(String key) {
  return getProperty(key, String.class, false);
 }
@Nullable
 protected  T getProperty(String key, Class targetValueType, boolean resolveNestedPlaceholders) {
  if (this.propertySources != null) {

   //例如遍历的是MutablePropertySources 的propertySourceList
   for (PropertySource<&#63;> propertySource : this.propertySources) {
    if (logger.isTraceEnabled()) {
     logger.trace("Searching for key '" + key + "' in PropertySource '" +
       propertySource.getName() + "'");
    }
    Object value = propertySource.getProperty(key);
    if (value != null) {
     //todo 解析 profile变量的时候 会去 解析 变量中的占位符 2020-09-11
     //TODO 解析xml配置文件路径字符串的时候 如果占位符 变量 的值 包含占位符 在这里 不会去解析 通过Helper 去解析 PropertyPlaceholderHelper
     if (resolveNestedPlaceholders && value instanceof String) {
      value = resolveNestedPlaceholders((String) value);
     }
     logKeyFound(key, propertySource, value);
     //跳出for 循环
     return convertValueIfNecessary(value, targetValueType);
    }
   }
  }
  if (logger.isTraceEnabled()) {
   logger.trace("Could not find key '" + key + "' in any property source");
  }
  return null;
 }

看到没有,我们是遍历this.propertySources集合,然后根据key调用它的getProperty方法获取value。我们从上面的StandardEnvrionment中看到我们定义的是 MapPropertySource 和 SystemEnvironmentPropertySource .

MapPropertySource

//从source中取得属性
@Override
@Nullable
public Object getProperty(String name) {
 return this.source.get(name);
}

这里的source就是getSystemProperties(),也就是 AbstractEnvironment中的方法:

@Override
@SuppressWarnings({"unchecked", "rawtypes"})
public Map getSystemProperties() {
 try {
  //Hashtable
  return (Map) System.getProperties();
 }
 catch (AccessControlException ex) {
  return (Map) new ReadOnlySystemAttributesMap() {
   @Override
   @Nullable
   protected String getSystemAttribute(String attributeName) {
   try {
    return System.getProperty(attributeName);
   }
   catch (AccessControlException ex) {
    if (logger.isInfoEnabled()) {
     logger.info("Caught AccessControlException when accessing system property '" +
      attributeName + "'; its value will be returned [null]. Reason: " + ex.getMessage());
    }
    return null;
   }
   }
  };
 }
}

我们还忘了很重要的一步,就是PropertyPlaceholderHelper的replacePlaceholders方法。

PropertyPlaceholderHelper

//protected 范围
protected String parseStringValue(
  String value, PlaceholderResolver placeholderResolver, Set visitedPlaceholders) {

 StringBuilder result = new StringBuilder(value);

 //如果value中没有占位符前缀 那直接返回result
 int startIndex = value.indexOf(this.placeholderPrefix);
 while (startIndex != -1) {
  //找到占位符的最后一个索引
  int endIndex = findPlaceholderEndIndex(result, startIndex);
  if (endIndex != -1) {
   String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
   String originalPlaceholder = placeholder;
   if (!visitedPlaceholders.add(originalPlaceholder)) {
   throw new IllegalArgumentException(
     "Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
   }

   //1. todo 2020-09-01 解析出来占位符,比如java.version
   //解析内嵌占位符
   // Recursive invocation, parsing placeholders contained in the placeholder key.
   placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
   // Now obtain the value for the fully resolved key...
   //2.todo 2020-09-01 获取实际值
   String propVal = placeholderResolver.resolvePlaceholder(placeholder);
   if (propVal == null && this.valueSeparator != null) {
   int separatorIndex = placeholder.indexOf(this.valueSeparator);
   if (separatorIndex != -1) {
    String actualPlaceholder = placeholder.substring(0, separatorIndex);
    String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
    //这里就是实际获取占位符中值得地方。
    propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);

   }
   }
  if (propVal != null) {
     //从占位符里获取的值也有可能包含占位符 这里可能会报 Circular placeholder reference
     propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
     //替换占位符 为 实际值
      result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
     if (logger.isTraceEnabled()) {
      logger.trace("Resolved placeholder '" + placeholder + "'");
     }
     startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
    }
   //省略部分代码
  }
  else {
   startIndex = -1;
  }
 }
 return result.toString();
}

到这里我们就可以看到Spring在处理一个小小的占位符就做了这么多设计。可见这个架构是如此严谨。下篇文章我们就来探讨下Spring是如何加载这个Xml文件的。

以上就是spring是如何解析xml配置文件中的占位符的详细内容,更多关于spring解析xml 占位符的资料请关注其它相关文章!


推荐阅读
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 阿里Treebased Deep Match(TDM) 学习笔记及技术发展回顾
    本文介绍了阿里Treebased Deep Match(TDM)的学习笔记,同时回顾了工业界技术发展的几代演进。从基于统计的启发式规则方法到基于内积模型的向量检索方法,再到引入复杂深度学习模型的下一代匹配技术。文章详细解释了基于统计的启发式规则方法和基于内积模型的向量检索方法的原理和应用,并介绍了TDM的背景和优势。最后,文章提到了向量距离和基于向量聚类的索引结构对于加速匹配效率的作用。本文对于理解TDM的学习过程和了解匹配技术的发展具有重要意义。 ... [详细]
  • Lodop中特殊符号打印设计和预览样式不同的问题解析
    本文主要解析了在Lodop中使用特殊符号打印设计和预览样式不同的问题。由于调用的本机ie引擎版本可能不同,导致在不同浏览器下样式解析不同。同时,未指定文字字体和样式设置也会导致打印设计和预览的差异。文章提出了通过指定具体字体和样式来解决问题的方法,并强调了以打印预览和虚拟打印机测试为准。 ... [详细]
  • Final关键字的含义及用法详解
    本文详细介绍了Java中final关键字的含义和用法。final关键字可以修饰非抽象类、非抽象类成员方法和变量。final类不能被继承,final类中的方法默认是final的。final方法不能被子类的方法覆盖,但可以被继承。final成员变量表示常量,只能被赋值一次,赋值后值不再改变。文章还讨论了final类和final方法的应用场景,以及使用final方法的两个原因:锁定方法防止修改和提高执行效率。 ... [详细]
  • 本文介绍了求解gcdexgcd斐蜀定理的迭代法和递归法,并解释了exgcd的概念和应用。exgcd是指对于不完全为0的非负整数a和b,gcd(a,b)表示a和b的最大公约数,必然存在整数对x和y,使得gcd(a,b)=ax+by。此外,本文还给出了相应的代码示例。 ... [详细]
  • EPICS Archiver Appliance存储waveform记录的尝试及资源需求分析
    本文介绍了EPICS Archiver Appliance存储waveform记录的尝试过程,并分析了其所需的资源容量。通过解决错误提示和调整内存大小,成功存储了波形数据。然后,讨论了储存环逐束团信号的意义,以及通过记录多圈的束团信号进行参数分析的可能性。波形数据的存储需求巨大,每天需要近250G,一年需要90T。然而,储存环逐束团信号具有重要意义,可以揭示出每个束团的纵向振荡频率和模式。 ... [详细]
  • VScode格式化文档换行或不换行的设置方法
    本文介绍了在VScode中设置格式化文档换行或不换行的方法,包括使用插件和修改settings.json文件的内容。详细步骤为:找到settings.json文件,将其中的代码替换为指定的代码。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 电销机器人作为一种人工智能技术载体,可以帮助企业提升电销效率并节省人工成本。然而,电销机器人市场缺乏统一的市场准入标准,产品品质良莠不齐。创业者在代理或购买电销机器人时应注意谨防用录音冒充真人语音通话以及宣传技术与实际效果不符的情况。选择电销机器人时需要考察公司资质和产品品质,尤其要关注语音识别率。 ... [详细]
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • 如何去除Win7快捷方式的箭头
    本文介绍了如何去除Win7快捷方式的箭头的方法,通过生成一个透明的ico图标并将其命名为Empty.ico,将图标复制到windows目录下,并导入注册表,即可去除箭头。这样做可以改善默认快捷方式的外观,提升桌面整洁度。 ... [详细]
  • 本文介绍了使用AJAX的POST请求实现数据修改功能的方法。通过ajax-post技术,可以实现在输入某个id后,通过ajax技术调用post.jsp修改具有该id记录的姓名的值。文章还提到了AJAX的概念和作用,以及使用async参数和open()方法的注意事项。同时强调了不推荐使用async=false的情况,并解释了JavaScript等待服务器响应的机制。 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
author-avatar
田小多
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有