版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/andy_zhang2007/article/details/84452098
概述
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);
}