1 Star 0 Fork 156

chendeyuan / Spring-Analysis

forked from huifer / Code-Analysis 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Spring-ManagedProperties.md 1.62 KB
一键复制 编辑 原始数据 按行查看 历史

Spring ManagedProperties

  • 类全路径: org.springframework.beans.factory.support.ManagedProperties

  • 类图: ManagedProperties.png

  • 对应标签 <props>

    <props>
           <prop key="administrator">administrator@example.org</prop>
           <prop key="support">support@example.org</prop>
           <prop key="development">development@example.org</prop>
    </props>
    • 解析xml的处理方法: org.springframework.beans.factory.xml.BeanDefinitionParserDelegate#parsePropsElement 这里不做具体展开

回到ManagedProperties类上, 一些get&set方法忽略, 这里对 merge 方法进行分析

方法列表

merge

  • ManagedProperties 作为Mergeable的实现类, 其操作的对象为Properties , 想到合并可以联想到 Properties#putAll =>java.util.Hashtable#putAll Spring 中也正是如此进行操作的. 详细代码如下
@Override
public Object merge(@Nullable Object parent) {
   // 是否允许合并
   if (!this.mergeEnabled) {
      throw new IllegalStateException("Not allowed to merge when the 'mergeEnabled' property is set to 'false'");
   }
   // 入参为空, 直接返回自己
   if (parent == null) {
      return this;
   }
   // 判断入参是否时 Properties
   if (!(parent instanceof Properties)) {
      throw new IllegalArgumentException("Cannot merge with object of type [" + parent.getClass() + "]");
   }
   // 创建 Properties , 通过两次 putAll 将数据填充返回
   Properties merged = new ManagedProperties();
   merged.putAll((Properties) parent);
   merged.putAll(this);
   return merged;
}
Java
1
https://gitee.com/chendeyuan/spring-analysis.git
git@gitee.com:chendeyuan/spring-analysis.git
chendeyuan
spring-analysis
Spring-Analysis
master

搜索帮助