本文共 5937 字,大约阅读时间需要 19 分钟。
PropertySources:持有一个或多个PropertySoucrce对象(如果对PropertySource不熟悉,建议看一下上一篇文章)
来看一下定义:
public interface PropertySources extends Iterable>
该接口实现了Iterable接口,那么也就是该资源是可迭代,可foreach等功能;然后本身还提供了:
default Stream> stream() { return StreamSupport.stream(spliterator(), false); } boolean contains(String name); @Nullable PropertySource get(String name);
通过这些接口信息我们可以看出,PropertySources主要提供对持有PropertySource提供操作扩展.
PropertySources仅有一个实现MutablePropertySources,因为这个类对理解spring环境比较重要,做一下详细说明,直接上源码:
public class MutablePropertySources implements PropertySources {//资源hold,一个在写时copy的数组链表,可以并发访问 private final List> propertySourceList = new CopyOnWriteArrayList<>(); /** * Create a new {@link MutablePropertySources} object. */ public MutablePropertySources() { } /** * Create a new {@code MutablePropertySources} from the given propertySources * object, preserving the original order of contained {@code PropertySource} objects. */ public MutablePropertySources(PropertySources propertySources) { this(); for (PropertySource propertySource : propertySources) { addLast(propertySource); } } @Override public Iterator > iterator() { return this.propertySourceList.iterator(); } @Override public Spliterator > spliterator() { return Spliterators.spliterator(this.propertySourceList, 0); } @Override public Stream > stream() { return this.propertySourceList.stream(); } @Override public boolean contains(String name) { return this.propertySourceList.contains(PropertySource.named(name)); } @Override @Nullable public PropertySource get(String name) { int index = this.propertySourceList.indexOf(PropertySource.named(name)); return (index != -1 ? this.propertySourceList.get(index) : null); } /** * Add the given property source object with highest precedence. */ public void addFirst(PropertySource propertySource) { removeIfPresent(propertySource); this.propertySourceList.add(0, propertySource); } /** * Add the given property source object with lowest precedence. */ public void addLast(PropertySource propertySource) { removeIfPresent(propertySource); this.propertySourceList.add(propertySource); } /** * Add the given property source object with precedence immediately higher * than the named relative property source. */ public void addBefore(String relativePropertySourceName, PropertySource propertySource) { assertLegalRelativeAddition(relativePropertySourceName, propertySource); removeIfPresent(propertySource); int index = assertPresentAndGetIndex(relativePropertySourceName); addAtIndex(index, propertySource); } /** * Add the given property source object with precedence immediately lower * than the named relative property source. */ public void addAfter(String relativePropertySourceName, PropertySource propertySource) { assertLegalRelativeAddition(relativePropertySourceName, propertySource); removeIfPresent(propertySource); int index = assertPresentAndGetIndex(relativePropertySourceName); addAtIndex(index + 1, propertySource); } /** * Return the precedence of the given property source, {@code -1} if not found. * 优先级获取,客户端使用时,会按照index从小到大获取属性,所以如果一个属性name,在多个属性资源中含有,那么优先级高的会使用 */ public int precedenceOf(PropertySource propertySource) { return this.propertySourceList.indexOf(propertySource); } /** * Remove and return the property source with the given name, {@code null} if not found. * @param name the name of the property source to find and remove */ @Nullable public PropertySource remove(String name) { int index = this.propertySourceList.indexOf(PropertySource.named(name)); return (index != -1 ? this.propertySourceList.remove(index) : null); } /** * Replace the property source with the given name with the given property source object. * @param name the name of the property source to find and replace * @param propertySource the replacement property source * @throws IllegalArgumentException if no property source with the given name is present * @see #contains * 在有些情况下如ServerContext,为了保证自己的优先级,会使用StubPropertySource进行占位,实际初始化后,再使用replace进行替换 */ public void replace(String name, PropertySource propertySource) { int index = assertPresentAndGetIndex(name); this.propertySourceList.set(index, propertySource); } /** * Return the number of {@link PropertySource} objects contained. */ public int size() { return this.propertySourceList.size(); } @Override public String toString() { return this.propertySourceList.toString(); } /** * Ensure that the given property source is not being added relative to itself. */ protected void assertLegalRelativeAddition(String relativePropertySourceName, PropertySource propertySource) { String newPropertySourceName = propertySource.getName(); if (relativePropertySourceName.equals(newPropertySourceName)) { throw new IllegalArgumentException( "PropertySource named '" + newPropertySourceName + "' cannot be added relative to itself"); } } /** * Remove the given property source if it is present. */ protected void removeIfPresent(PropertySource propertySource) { this.propertySourceList.remove(propertySource); } /** * Add the given property source at a particular index in the list. */ private void addAtIndex(int index, PropertySource propertySource) { removeIfPresent(propertySource); this.propertySourceList.add(index, propertySource); } /** * Assert that the named property source is present and return its index. * @param name {@linkplain PropertySource#getName() name of the property source} to find * @throws IllegalArgumentException if the named property source is not present */ private int assertPresentAndGetIndex(String name) { int index = this.propertySourceList.indexOf(PropertySource.named(name)); if (index == -1) { throw new IllegalArgumentException("PropertySource named '" + name + "' does not exist"); } return index; }}
转载地址:http://zduti.baihongyu.com/