spring中PropertyPlaceholderConfigurer和PropertyOverrideConfigurer的使用
2015-02-07 02:44
337 查看
先上类图:
均实现了BeanFactoryPostProcessor,作为容器的后置处理器---时机发生在配置文件加载完成之后,bean实例化之前。
而PriorityOrdered和PropertyResourceConfigurer组合实现order书序,描述了子类之间的加载顺序。
继承PropertiesLoaderSupport类,主要属性为:
locations--配置文件的位置。
properties--配置项的默认值,可以被locations中的配置项覆盖。
localOverride:是否由默认配置项覆盖locations中的配置项,默认为false
该类通过设置启用systemPropertiesMode和searchSystemEnvironment属性来控制占位符的值是否会到JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。
SystemPropertiesModeName属性用来指定系统变量(System.getProperty)是否覆盖proprties文件。默认是在配置文件里找不到时使用。三个选项分别是:
SYSTEM_PROPERTIES_MODE_NEVER(不覆盖)
SYSTEM_PROPERTIES_MODE_FALLBACK(默认值,不存在时覆盖)
SYSTEM_PROPERTIES_MODE_OVERRIDE(覆盖)
searchSystemEnvironment属性的默认值为true,即允许环境变量覆盖properties中的值。
这里以spring-batch默认的配置文件作为示例:
ignoreInvalidKeys用来决定是否忽略不符合beanName.filedName这种规范的key,默认值是false,这个参数主要用来满足一个properties文件中可以放置任意类型的key。
spring-batch配置文件的示例:
<bean id="overrideProperties" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"
depends-on="environment">
<property name="location" value="classpath:batch-${ENVIRONMENT}.properties" />
<!-- Allow system properties (-D) to override those from file -->
<property name="localOverride" value="true" />
<property name="properties">
<bean class="java.lang.System" factory-method="getProperties" />
</property>
<property name="ignoreInvalidKeys" value="true" />
<property name="order" value="2" />
</bean>
均实现了BeanFactoryPostProcessor,作为容器的后置处理器---时机发生在配置文件加载完成之后,bean实例化之前。
而PriorityOrdered和PropertyResourceConfigurer组合实现order书序,描述了子类之间的加载顺序。
继承PropertiesLoaderSupport类,主要属性为:
locations--配置文件的位置。
properties--配置项的默认值,可以被locations中的配置项覆盖。
localOverride:是否由默认配置项覆盖locations中的配置项,默认为false
PropertyPlaceholderConfigurer
PropertyPlaceholderConfigurer是负责读取location属性配置的的properties属性文件里的属性值,并将这些属性值设置成Spring配置文件中的占位符里。该类通过设置启用systemPropertiesMode和searchSystemEnvironment属性来控制占位符的值是否会到JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。
SystemPropertiesModeName属性用来指定系统变量(System.getProperty)是否覆盖proprties文件。默认是在配置文件里找不到时使用。三个选项分别是:
SYSTEM_PROPERTIES_MODE_NEVER(不覆盖)
SYSTEM_PROPERTIES_MODE_FALLBACK(默认值,不存在时覆盖)
SYSTEM_PROPERTIES_MODE_OVERRIDE(覆盖)
searchSystemEnvironment属性的默认值为true,即允许环境变量覆盖properties中的值。
这里以spring-batch默认的配置文件作为示例:
<bean id="placeholderProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" depends-on="environment"> <property name="location" value="classpath:batch-${ENVIRONMENT}.properties" /> <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" /> <property name="ignoreUnresolvablePlaceholders" value="true" /> <property name="order" value="1" /> </bean>
PropertyOverrideConfigurer
PropertyOverrideConfigurer的属性文件指定的信息可以直接覆盖Spring配置文件中原默认值(不管这个属性是否使用了占位符以及是否存在)。ignoreInvalidKeys用来决定是否忽略不符合beanName.filedName这种规范的key,默认值是false,这个参数主要用来满足一个properties文件中可以放置任意类型的key。
spring-batch配置文件的示例:
<bean id="overrideProperties" class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"
depends-on="environment">
<property name="location" value="classpath:batch-${ENVIRONMENT}.properties" />
<!-- Allow system properties (-D) to override those from file -->
<property name="localOverride" value="true" />
<property name="properties">
<bean class="java.lang.System" factory-method="getProperties" />
</property>
<property name="ignoreInvalidKeys" value="true" />
<property name="order" value="2" />
</bean>
相关文章推荐
- Spring中PropertyPlaceholderConfigurer的使用
- spring中使用外部属性文件(关于PropertyPlaceholderConfigurer)
- 关于系统中使用多个PropertyPlaceholderConfigurer的配置及PropertyOverrideConfigurer
- Spring PropertyPlaceholderConfigurer Usage - 使用系统变量替换spring配置文件中的变量
- Spring中PropertyPlaceholderConfigurer的使用
- spring使用多个PropertyPlaceholderConfigurer的配置
- Spring中PropertyPlaceholderConfigurer的使用
- Spring:PropertyPlaceholderConfigurer的使用
- Spring中PropertyPlaceholderConfigurer的使用
- Spring中PropertyPlaceholderConfigurer的使用(一)
- Spring中PropertyPlaceholderConfigurer的使用
- spring使用PropertyPlaceholderConfigurer扩展来满足不同环境的参数配置
- Spring学习笔记 通过PropertyPlaceholderConfigurer来使用properties文件初始化Map类型属性
- Spring配置文件中使用PropertyPlaceholderConfigurer配置获取jdbc的配置信息
- Spring PropertyPlaceholderConfigurer的使用
- spring使用PropertyPlaceholderConfigurer扩展来满足不同环境的参数配置
- Spring PropertyPlaceholderConfigurer Usage - 使用系统变量替换spring配置文件中的变量
- Spring中PropertyPlaceholderConfigurer的使用(一)
- Spring中PropertyPlaceholderConfigurer的使用
- Spring的PropertyPlaceholderConfigurer使用