Spring3.1提供了新的属性管理API,而且功能非常强大且很完善,对于一些属性配置信息都应该使用新的API来管理。虽然现在Spring已经到4版本了,这篇文章来的晚点。
新的属性管理API
PropertySource:属性源,key-value属性对抽象,比如用于配置数据
PropertyResolver:属性解析器,用于解析相应key的value
Environment:环境,本身是一个PropertyResolver,但是提供了Profile特性,即可以根据环境得到相应数据(即激活不同的Profile,可以得到不同的属性数据,比如用于多环境场景的配置(正式机、测试机、开发机DataSource配置))
Profile:剖面,只有激活的剖面的组件/配置才会注册到Spring容器,类似于maven中profile
也就是说,新的API主要从配置属性、解析属性、不同环境解析不同的属性、激活哪些组件/配置进行注册这几个方面进行了重新设计,使得API的目的更加清晰,而且功能更加强大。
PropertySource
key-value对,API如下所示:
public String getName() //属性源的名字 public T getSource() //属性源(比如来自Map,那就是一个Map对象) public boolean containsProperty(String name) //是否包含某个属性 public abstract Object getProperty(String name) //得到属性名对应的属性值
非常类似于Map;用例如下:
@Test
public void test() throws IOException {
Map
map = new HashMap<>();
map.put("encoding", "gbk");
PropertySource propertySource1 = new MapPropertySource("map", map);
System.out.println(propertySource1.getProperty("encoding"));
ResourcePropertySource propertySource2 = new ResourcePropertySource("resource", "classpath:resources.properties"); //name, location
System.out.println(propertySource2.getProperty("encoding"));
}
MapPropertySource的属性来自于一个Map,而ResourcePropertySource的属性来自于一个properties文件,另外还有如PropertiesPropertySource,其属性来自Properties,ServletContextPropertySource的属性来自ServletContext上下文初始化参数等等,大家可以查找PropertySource的继承层次查找相应实现。
@Test
public void test2() throws IOException {
//省略propertySource1/propertySource2
CompositePropertySource compositePropertySource = new CompositePropertySource("composite");
compositePropertySource.addPropertySource(propertySource1);
compositePropertySource.addPropertySource(propertySource2);
System.out.println(compositePropertySource.getProperty("encoding"));
}
CompositePropertySource提供了组合PropertySource的功能,查找顺序就是注册顺序。
另外还有一个PropertySources,从名字可以看出其包含多个PropertySource:
public interface PropertySources extends Iterable> { boolean contains(String name); //是否包含某个name的PropertySource PropertySource get(String name); //根据name找到PropertySource }
示例如下:
@Test
public void test3() throws IOException {
//省略propertySource1/propertySource2
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addFirst(propertySource1);
propertySources.addLast(propertySource2);
System.out.println(propertySources.get("resource").getProperty("encoding"));
for(PropertySource propertySource : propertySources) {
System.out.println(propertySource.getProperty("encoding"));
}
}
默认提供了一个MutablePropertySources实现,我们可以调用addFirst添加到列表的开头,addLast添加到末尾,另外可以通过addBefore(propertySourceName, propertySource)或addAfter(propertySourceName, propertySource)添加到某个propertySource前面/后面;最后大家可以通过iterator迭代它,然后按照顺序获取属性。
到目前我们已经有属性了,接下来需要更好的API来解析属性了。
PropertyResolver
属性解析器,用来根据名字解析其值等。API如下所示:
public interface PropertyResolver {
//是否包含某个属性
boolean containsProperty(String key);
//获取属性值 如果找不到返回null
String getProperty(String key);
//获取属性值,如果找不到返回默认值
String getProperty(String key, String defaultValue);
//获取指定类型的属性值,找不到返回null
T getProperty(String key, Class
targetType); //获取指定类型的属性值,找不到返回默认值
T getProperty(String key, Class
targetType