PropertySource
这是一个抽象类,在org.springframework.core.env包中,这个类表示key-value形式的数据
既然是这样,我们直接用java.util自带的Properties不好吗,当然也好,不过spring的PropertySource提供了诸多实现,主要是用来将不同的源,转换成Properties,如果我们用Properties,那么需要调用load方法,自己创建InputStream
而使用PropertySource,我们就可以直接用spring实现好的类,少很多代码,比如,我想将一个文件转换成Properties,则可是直接使用ResourcePropertySource
如果我想将我写的一个HashMap转换成Properties,则直接可以使用MapPropertySource,代码示例如下:
Map<String, Object> map = new HashMap<>();
map.put("aaa", "ABC");
PropertySource propertySource = new MapPropertySource("name1", map);
String value = propertySource.getProperty("aaa");
System.out.println(value);// 这行会打印"ABC"
PropertySources
这是一个接口,其实就是实现了Iterable的PropertySource集合,比较容易理解,截止到spring 3.1,这个接口只有一个实现类,就是MutablePropertySources