Spring 系列目录(http://www.javashuo.com/article/p-kqecupyl-bm.html)html
Spring 3.1 提供了新的占位符解析器 PropertyResolver,默认实现为 PropertySourcesPropertyResolver。相关文章以下:java
PropertyResolver 的默认实现是 PropertySourcesPropertyResolver,Environment 实际上也是委托 PropertySourcesPropertyResolver 完成 占位符的解析和类型转换。 类型转换又是委托 ConversionService 完成的。spring
public interface PropertyResolver { // 1. contains boolean containsProperty(String key); // 2.1 获取指定 key,不存在能够指定默认值,也能够抛出异常 String getProperty(String key); String getProperty(String key, String defaultValue); // 2.2 类型转换,委托 ConversionService 完成 <T> T getProperty(String key, Class<T> targetType); <T> T getProperty(String key, Class<T> targetType, T defaultValue); String getRequiredProperty(String key) throws IllegalStateException; <T> T getRequiredProperty(String key, Class<T> targetType) throws IllegalStateException; // 3. 解析占位符 ${key} String resolvePlaceholders(String text); String resolveRequiredPlaceholders(String text) throws IllegalArgumentException; }
PropertyResolver 组件完成了两件事:一是占位符解析 ${key};二是类型转换。使用方法以下:api
@Test public void PropertyResolverTest() { PropertySource propertySource = new MapPropertySource("source", Collections.singletonMap("name", "binarylei")); MutablePropertySources propertySources = new MutablePropertySources(); propertySources.addFirst(propertySource); PropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources); Assert.assertEquals("binarylei", propertyResolver.getProperty("name")); Assert.assertEquals("name is binarylei", propertyResolver.resolvePlaceholders("name is ${name}")); }
(1) xml 配置ide
<context:property-placeholder location="属性文件,多个之间逗号分隔" file-encoding="文件编码" ignore-resource-not-found="是否忽略找不到的属性文件" ignore-unresolvable="是否忽略解析不到的属性,若是不忽略,找不到将抛出异常" properties-ref="本地Properties配置" local-override="是否本地覆盖模式,即若是true,那么properties-ref的属性将覆盖location加载的属性,不然相反" system-properties-mode="系统属性模式,默认ENVIRONMENT(表示先找ENVIRONMENT,再找properties-ref/location的),NEVER:表示永远不用ENVIRONMENT的,OVERRIDE相似于ENVIRONMENT" order="顺序" />
location
:表示属性文件位置,多个之间经过如逗号/分号等分隔;file-encoding
:文件编码;ignore-resource-not-found
:若是属性文件找不到,是否忽略,默认 false,即不忽略,找不到将抛出异常ignore-unresolvable
:是否忽略解析不到的属性,若是不忽略,找不到将抛出异常properties-ref
:本地 java.util.Properties 配置local-override
:是否本地覆盖模式,即若是 true,那么 properties-ref 的属性将覆盖 location 加载的属性system-properties-mode
:系统属性模式,ENVIRONMENT(默认),NEVER,OVERRIDE
ENVIRONMENT
:将使用 Spring 3.1 提供的 PropertySourcesPlaceholderConfigurer,其余状况使用 Spring 3.1 以前的 PropertyPlaceholderConfigurer。若是是本地覆盖模式:那么查找顺序是:properties-ref、location、environment,不然正好反过来;OVERRIDE
: PropertyPlaceholderConfigurer 使用,由于在 spring 3.1 以前版本是没有 Enviroment 的,因此 OVERRIDE 是 spring 3.1 以前版本的 Environment。若是是本地覆盖模式:那么查找顺序是:properties-ref、location、System.getProperty(), System.getenv(),不然正好反过来;NEVER
:只查找 properties-ref、location;order
:当配置多个
(2) 注解配置源码分析
@Configuration @PropertySource(value = "classpath:resources.properties", ignoreResourceNotFound = false) public class AppConfig { // 若是想进行 Bean 属性的占位符替换,须要注册 PropertySourcesPlaceholderConfigurer @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } }
如上配置等价于 XML 中的
(3) 占位符替换编码
使用 Environment 属性替换,如:spa
<context:property-placeholder location="classpath:${env}/resources.properties"/> <context:component-scan base-package="com.sishuok.${package}"/> <import resource="classpath:${env}/ctx.xml"/>
一样能够在注解中使用占位符。code
@PropertySource(value = "classpath:${env}/resources.properties") @ComponentScan(basePackages = "com.sishuok.${package}") @ImportResource(value = {"classpath:${env}/cfg.xml"}) @Value("${env}") new ClassPathXmlApplicationContext("classpath:${env}/cfg.xml")
参考:
天天用心记录一点点。内容也许不重要,但习惯很重要!