概述
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及其集合管理PropertySources,介绍如何统一管理和访问不同类型的属性源,包括属性文件、环境变量等,使Spring应用能一致地访问所有属性源。
483

被折叠的 条评论
为什么被折叠?



