1. 程式人生 > >Spring PropertySources抽象多屬性源

Spring PropertySources抽象多屬性源

概述

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); }

相關文章

Spring屬性源抽象PropertySource