PropertySource 类详解

image.png
PropertySource 类图

PropertySource

PropertySource 类 设计用来存放<key,value>对象的抽象类。同时name,source都是final在初始化后不在变化,

protected final String name;
protected final T source;

getProperty 是个抽象方法,需要在子类来实现。

equals 方法解读,

     // 当对比对象与 当前对象恒等,或者name相等,则判断相等。
    @Override
    public boolean equals(Object obj) {
        return (this == obj || (obj instanceof PropertySource &&
                ObjectUtils.nullSafeEquals(this.name, ((PropertySource<?>) obj).name)));
    }

named()

    //  根据name 创建一个 子类 ComparisonPropertySource 对象
    public static PropertySource<?> named(String name) {
        return new ComparisonPropertySource(name);
    }

ComparisonPropertySource 静态内部类

     // StubPropertySource  继承自  PropertySource 实现  `getProperty` 返回null 

   // ComparisonPropertySource  继承 StubPropertySource    除构造函数为全部抛出异常,一般用来创建一个具名 PropertySource
    static class ComparisonPropertySource extends StubPropertySource {

        private static final String USAGE_ERROR =
                "ComparisonPropertySource instances are for use with collection comparison only";

        public ComparisonPropertySource(String name) {
            super(name);
        }

        @Override
        public Object getSource() {
            throw new UnsupportedOperationException(USAGE_ERROR);
        }

        @Override
        public boolean containsProperty(String name) {
            throw new UnsupportedOperationException(USAGE_ERROR);
        }

        @Override
        public String getProperty(String name) {
            throw new UnsupportedOperationException(USAGE_ERROR);
        }
    }

示例代码


List<PropertySource<?>> sources = new ArrayList<PropertySource<?>>();
      sources.add(new MapPropertySource("sourceA", mapA));
      sources.add(new MapPropertySource("sourceB", mapB));
       sources.contains(PropertySource.named("sourceA"));
      sources.contains(PropertySource.named("sourceB"));
     !sources.contains(PropertySource.named("sourceC"));
     }
PropertySource   类重写了equals方法,named 创建一个具名的 对象

image.png

EnumerablePropertySource

    //  重写 containsProperty  方法
    @Override
    public boolean containsProperty(String name) {
        return ObjectUtils.containsElement(getPropertyNames(), name);
    }


//   添抽样 方法,用来返回 value的 name
public abstract String[] getPropertyNames();

MapPropertySource

image.png

以Map的形式保存 source

public class MapPropertySource extends EnumerablePropertySource<Map<String, Object>> {



    public MapPropertySource(String name, Map<String, Object> source) {
        super(name, source);
    }
    @Override
    public Object getProperty(String name) {
        return this.source.get(name);
    }

    @Override
    public boolean containsProperty(String name) {
        return this.source.containsKey(name);
    }

    @Override
    public String[] getPropertyNames() {
        return StringUtils.toStringArray(this.source.keySet());
    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,156评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,805评论 18 399
  • 一、基础知识:1、JVM、JRE和JDK的区别:JVM(Java Virtual Machine):java虚拟机...
    杀小贼阅读 2,441评论 0 4
  • 为了节约开发成本,很多Native-H5混合App采用手机网站支付的方式去实现支付模块。但手机网站支付的网络依赖比...
    Swift社区阅读 1,711评论 16 4
  • 进门有门槛 达到门槛才能进 我也很想去清华大学学习
    MorganWoo阅读 90评论 0 0