Spring PropertySources抽象多属性源

本文深入探讨Spring框架中属性源的抽象模型PropertySource及其集合管理PropertySources,介绍如何统一管理和访问不同类型的属性源,包括属性文件、环境变量等,使Spring应用能一致地访问所有属性源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

概述

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值