Spring PropertySources抽象多屬性源
阿新 • • 發佈:2018-12-03
概述
Spring
應用會遇到各種各樣的屬性源:屬性檔案,System.getenv() Map
, Sytem.getProperties() Properties
,某個Map/Properties
物件等等。通過將各種型別的屬性源通過介面PropertySource
進行抽象建模,一個屬性源最終可以表示為一個PropertySource
物件,而Spring應用也可以一致地訪問對所有屬性源了。不僅如此,Spring還對一組PropertySource
作了進一步的封裝抽象,這就是介面PropertySources
,從而進一步封裝和隱藏屬性源訪問操作的細節。比如Spring在實現對執行環境的建模時,就使用了一個PropertySources
原始碼分析
package org.springframework.core.env;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import org.springframework.lang.Nullable;
/**
* Holder containing one or more PropertySource objects.
* 封裝一個或者多個 PropertySource 物件
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
* @see PropertySource
*/
public interface PropertySources extends Iterable<PropertySource<?>> {
/**
* Return a Stream containing the property sources.
*
* 對所包含的PropertySource物件的流式操作支援,返回一個流Stream
* @since 5.1
*/
default Stream<PropertySource<?>> stream() {
return StreamSupport.stream(spliterator (), false);
}
/**
* Return whether a property source with the given name is contained.
* 該物件是否包含一個名稱為name的PropertySource物件
* @param name the name of the property source to find (PropertySource#getName())
*/
boolean contains(String name);
/**
* Return the property source with the given name, null if not found.
* 獲取指定名稱為name的PropertySource物件,如果該物件不包含該名稱的PropertySource物件,返回null
* @param name the name of the property source to find (PropertySource#getName())
*/
@Nullable
PropertySource<?> get(String name);
}