spring-env/spring-env-propertySources/README.md
✒️作者- Lex 📝博客- 掘金 📚源码地址- github
PropertySource
getProperty(String name) 方法,它根据提供的属性名来检索属性值。在 Spring 的环境抽象中,PropertySource 的实例可以被添加到 Environment 对象中,从而允许我们在应用程序中方便地访问和管理这些属性。PropertySources 是一个Spring框架中的接口,用于表示和管理一组属性源(PropertySource对象),这些属性源包含了应用程序环境中的配置数据。该接口提供了一系列方法来检索、添加、替换和删除这些属性源,允许开发者以统一的方式访问不同来源的配置信息,如环境变量、系统属性、配置文件等。
属性源管理
属性检索
属性访问
属性源枚举
层次化属性源
属性源自定义
PropertySources,提供更特定的属性源管理策略。PropertySources 是一个在Spring框架中定义的接口,旨在作为一个容器,管理和封装一组 PropertySource 对象。这些对象代表了应用程序中的各种属性源,如环境变量、系统属性或配置文件。接口提供了流式访问这些属性源的功能,允许开发者以顺序流的形式对属性源进行操作。它还包括用于检查特定属性源是否存在的方法,以及根据名称检索属性源的能力
/**
* 包含一个或多个 {@link PropertySource} 对象的容器。
*
* @author Chris Beams
* @author Juergen Hoeller
* @since 3.1
* @see PropertySource
*/
public interface PropertySources extends Iterable<PropertySource<?>> {
/**
* 返回一个包含属性源的顺序 {@link Stream}。
* @since 5.1
*/
default Stream<PropertySource<?>> stream() {
return StreamSupport.stream(spliterator(), false);
}
/**
* 返回是否包含给定名称的属性源。
* @param name 要找的属性源的名称
*/
boolean contains(String name);
/**
* 返回给定名称的属性源,如果未找到则返回 null。
* @param name 要找的属性源的名称
*/
@Nullable
PropertySource<?> get(String name);
}
MutablePropertySources
使用 Spring 的 MutablePropertySources 来管理应用配置属性的过程。它创建了一个属性源集合,向其中添加、替换和移除了不同的属性源,并展示了如何检查属性源的存在性。
public class PropertySourcesDemo {
public static void main(String[] args) {
// 创建 MutablePropertySources 对象
MutablePropertySources propertySources = new MutablePropertySources();
// 创建两个 MapPropertySource 对象
Map<String, Object> config1 = new HashMap<>();
config1.put("key1", "value1");
PropertySource<?> mapPropertySource1 = new MapPropertySource("config1", config1);
Map<String, Object> config2 = new HashMap<>();
config2.put("key2", "value2");
PropertySource<?> mapPropertySource2 = new MapPropertySource("config2", config2);
// 添加属性源到开头
propertySources.addFirst(mapPropertySource1);
// 添加属性源到末尾
propertySources.addLast(mapPropertySource2);
// 打印
System.out.println("打印属性源");
for (PropertySource<?> ps : propertySources) {
System.out.printf("Name: %-10s || Source: %s%n", ps.getName(), ps.getSource());
}
System.out.println();
// 替换属性源
Map<String, Object> newConfig = new HashMap<>();
newConfig.put("app.name", "Spring-Reading");
newConfig.put("app.version", "1.0.0");
PropertySource<?> newMapPropertySource = new MapPropertySource("config1", newConfig);
propertySources.replace("config1", newMapPropertySource);
// 打印替换后
System.out.println("打印替换后的属性源");
for (PropertySource<?> ps : propertySources) {
System.out.printf("Name: %-10s || Source: %s%n", ps.getName(), ps.getSource());
}
System.out.println();
// 检查是否包含属性源
System.out.println("检���是否包含属性源 config2: " + propertySources.contains("config2"));
// 移除属性源
System.out.println("移除属性源 config2: " + propertySources.remove("config2"));
// 再次检查是否包含属性源
System.out.println("删除后是否包含属性源 config2: " + propertySources.contains("config2"));
}
}
运行结果发现,MutablePropertySources 如何灵活地管理属性源,包括添加、替换和移除操作,以及如何查询属性源的存在性。
打印属性源
Name: config1 || Source: {key1=value1}
Name: config2 || Source: {key2=value2}
打印替换后的属性源
Name: config1 || Source: {app.name=Spring-Reading, app.version=1.0.0}
Name: config2 || Source: {key2=value2}
检查是否包含属性源 config2: true
移除属性源 config2: MapPropertySource {name='config2'}
删除后是否包含属性源 config2: false
Environment
PropertySources 是 Environment 接口的核心组成部分,负责提供应用程序的配置数据。在 Environment 中,PropertySources 被用来搜索和访问不同来源的属性值,如系统属性、环境变量和配置文件,从而实现统一的配置属性访问机制。PropertySource
PropertySources 管理一组 PropertySource 对象,其中每个 PropertySource 代表了一个单独的属性源,如配置文件或环境变量集。PropertySources 为这些不同的属性源提供集中式的访问和管理能力。PropertyResolver
PropertySources 与 PropertyResolver 接口紧密相关,后者提供属性值的解析功能,例如处理占位符。在处理和转换属性值时,PropertySources 通常与 PropertyResolver 结合使用,以提高属性管理的灵活性和效率。ApplicationContext
ApplicationContext 中,PropertySources 被用于配置和管理应用程序的环境属性。它与应用上下文交互,允许灵活定义和访问应用程序的配置设置,从而成为 Spring 应用配置的重要组成部分。Profile
PropertySources 与 Profile 相关联,后者用于定义特定环境的配置。通过 PropertySources,可以存储和访问与特定 Profile 相关的属性,同时在应用启动时激活或禁用特定的 Profile,控制配置的加载。属性值重复或覆盖
PropertySources 中各个属性源的优先级和顺序,确保期望的属性源具有正确的优先级。属性值未找到
环境依赖性问题
属性类型不匹配
属性源更新问题
MutablePropertySources 并确保适当的同步和配置刷新,或考虑使用外部配置管理系统,可以解决这一问题。属性解析问题