Properties、CompositePropertySource、MapPropertySource、PropertiesPropertySource和PropertySource
Properties是Java中的一个类,用于管理属性键值对的集合。它是一个简单的键值对容器,常用于读取和写入配置文件。
Properties props = new Properties();
props.load(new FileInputStream("config.properties"));
String value = props.getProperty("key");
PropertySource是Spring框架中的一个接口,用于表示属性源。它提供了获取属性值的方法,并且可以根据属性名称进行查找。PropertySource是一个抽象概念,可以有多种实现方式。
public class MyPropertySource extends PropertySource<String> {
public MyPropertySource(String name) {
super(name);
}
@Override
public Object getProperty(String name) {
// 从某个地方获取属性值
return value;
}
}
MapPropertySource是PropertySource的一个具体实现,它使用一个Map来存储属性键值对。通过MapPropertySource,我们可以将一个Map对象作为属性源,方便地管理和访问属性值。
Map<String, Object> map = new HashMap<>();
map.put("key1", "value1");
map.put("key2", "value2");
PropertySource propertySource = new MapPropertySource("myMapPropertySource", map);
PropertiesPropertySource是MapPropertySource的一个子类,它使用Properties对象作为属性源。它将Properties对象中的属性键值对转换为Map,并提供了与MapPropertySource相同的功能。
Properties props = new Properties();
props.load(new FileInputStream("config.properties"));
PropertySource propertySource = new PropertiesPropertySource("myPropertiesPropertySource", props);
CompositePropertySource是PropertySource的另一个具体实现,它可以将多个PropertySource组合在一起,形成一个属性源的层次结构。通过CompositePropertySource,我们可以将多个属性源进行组合,实现属性值的继承和覆盖。
CompositePropertySource compositePropertySource = new CompositePropertySource("myCompositePropertySource");
compositePropertySource.addPropertySource(new MapPropertySource("myMapPropertySource", map));
compositePropertySource.addPropertySource(new PropertiesPropertySource("myPropertiesPropertySource", props));
它们之间的关系是:
- Properties是一个简单的属性键值对容器,常用于读取和写入配置文件。
- PropertySource是一个抽象概念,表示属性源,可以有多种实现方式。
- MapPropertySource是PropertySource的一个具体实现,使用Map来存储属性键值对。
- PropertiesPropertySource是MapPropertySource的一个子类,使用Properties对象作为属性源。
- CompositePropertySource是PropertySource的另一个具体实现,可以将多个PropertySource组合在一起,形成一个属性源的层次结构。
CompositePropertySource和PropertySource是Java中用于管理属性源的类。
CompositePropertySource类的常用方法包括:
1. getProperty(String name): 获取指定名称的属性值。
2. getPropertyNames(): 获取所有属性名称的列表。
3. containsProperty(String name): 判断是否包含指定名称的属性。
4. getSource(): 获取属性源的原始对象。
5. getName(): 获取属性源的名称。
6. getPropertySources(): 获取所有子属性源的列表。
7. addFirstPropertySource(PropertySource<?> propertySource): 将指定属性源添加到子属性源列表的最前面。
8. addLastPropertySource(PropertySource<?> propertySource): 将指定属性源添加到子属性源列表的最后面。
9. equals(Object obj): 判断当前属性源是否等于指定对象。
10. hashCode(): 获取当前属性源的哈希码。
CompositePropertySource是一个组合多个PropertySource的容器,可以将多个PropertySource合并为一个,方便统一管理和使用。示例代码:
import org.springframework.core.env.*;
public class CompositePropertySourceExample {
public static void main(String[] args) {
// 创建一个CompositePropertySource对象
CompositePropertySource compositePropertySource = new CompositePropertySource("myCompositePropertySource");
// 创建PropertiesPropertySource对象
Properties properties = new Properties();
properties.setProperty("key1", "value1");
PropertySource<?> propertiesPropertySource = new PropertiesPropertySource("myPropertiesPropertySource", properties);
// 创建MapPropertySource对象
MapPropertySource mapPropertySource = new MapPropertySource("myMapPropertySource", Map.of("key2", "value2"));
// 将PropertiesPropertySource和MapPropertySource添加到CompositePropertySource中
compositePropertySource.addPropertySource(propertiesPropertySource);
compositePropertySource.addPropertySource(mapPropertySource);
// 使用CompositePropertySource获取属性值
Environment environment = new StandardEnvironment();
((ConfigurableEnvironment) environment).getPropertySources().addFirst(compositePropertySource);
System.out.println(environment.getProperty("key1")); // 输出:value1
System.out.println(environment.getProperty("key2")); // 输出:value2
}
}
在上面的示例中,我们创建了一个CompositePropertySource对象,并分别创建了一个PropertiesPropertySource和一个MapPropertySource对象。然后将这两个PropertySource对象添加到CompositePropertySource中。最后,我们使用CompositePropertySource获取属性值,可以看到成功获取到了key1和key2对应的值。
示例展示了CompositePropertySource与PropertiesPropertySource和MapPropertySource的结合使用,也可以根据需要使用其他类型的PropertySource对象。
常用的PropertySource实现类
- MapPropertySource:从一个Map对象中加载属性。
Map<String, Object> properties = new HashMap<>();
properties.put("key1", "value1");
properties.put("key2", "value2");
PropertySource<Map<String, Object>> propertySource = new MapPropertySource("mapPropertySource", properties);
- SystemEnvironmentPropertySource:从系统环境变量中加载属性。
PropertySource<?> propertySource = new SystemEnvironmentPropertySource("systemEnvironmentPropertySource");
- SystemPropertiesPropertySource:从系统属性中加载属性。
PropertySource<?> propertySource = new SystemPropertiesPropertySource("systemPropertiesPropertySource");
- ResourcePropertySource:从资源文件中加载属性。
示例代码:
Resource resource = new ClassPathResource("config.properties");
PropertySource<?> propertySource = new ResourcePropertySource("resourcePropertySource", resource);
Resource
Resource是 Spring Framework 中用于表示资源的接口。它可以表示文件、类路径资源、URL 等不同类型的资源。Resource 接口提供了一些方法,例如 getInputStream()
,用于获取资源的输入流。
常用的 Resource 实现类有以下几种:
- ClassPathResource:用于访问类路径下的资源,如配置文件、XML 文件等。
Resource resource = new ClassPathResource("config.properties");
InputStream inputStream = resource.getInputStream();
- FileSystemResource:用于访问文件系统中的资源,如本地磁盘上的文件。
Resource resource = new FileSystemResource("C:/data/config.properties");
InputStream inputStream = resource.getInputStream();
- UrlResource:用于访问 URL 地址上的资源,如网络上的文件。
Resource resource = new UrlResource("http://example.com/config.properties");
InputStream inputStream = resource.getInputStream();
- ByteArrayResource:用于访问字节数组类型的资源。
byte[] data = "hello world".getBytes();
Resource resource = new ByteArrayResource(data);
InputStream inputStream = resource.getInputStream();
- InputStreamResource:用于访问输入流类型的资源。
InputStream inputStream = new FileInputStream("C:/data/config.properties");
Resource resource = new InputStreamResource(inputStream);
MutablePropertySources
MutablePropertySources
是Spring框架中的一个类,用于管理应用程序的属性源。它允许您添加、删除和排序属性源,以便在应用程序中使用不同的属性源。
常用方法包括:
addFirst(PropertySource<?> propertySource)
:将属性源添加到列表的开头。addLast(PropertySource<?> propertySource)
:将属性源添加到列表的末尾。addBefore(String relativePropertySourceName, PropertySource<?> propertySource)
:在指定的属性源之前添加一个新的属性源。addAfter(String relativePropertySourceName, PropertySource<?> propertySource)
:在指定的属性源之后添加一个新的属性源。remove(PropertySource<?> propertySource)
:从列表中删除指定的属性源。replace(String name, PropertySource<?> propertySource)
:用新的属性源替换指定名称的属性源。
下面是一个简单的示例,演示如何使用MutablePropertySources
类:
import org.springframework.core.env.MutablePropertySources;
import org.springframework.core.env.PropertySource;
public class MyApp {
public static void main(String[] args) {
MutablePropertySources propertySources = new MutablePropertySources();
// 添加一个属性源
PropertySource<?> myPropertySource = new MyPropertySource();
propertySources.addLast(myPropertySource);
// 添加另一个属性源
PropertySource<?> anotherPropertySource = new AnotherPropertySource();
propertySources.addLast(anotherPropertySource);
// 删除属性源
propertySources.remove(myPropertySource);
// 替换属性源
PropertySource<?> newPropertySource = new NewPropertySource();
propertySources.replace("myPropertySource", newPropertySource);
}
}
在这个示例中,我们创建了一个MutablePropertySources
对象,并添加了两个属性源。然后,我们删除了一个属性源,并用另一个属性源替换了一个属性源。