Spring(12)——PropertyPlaceholderConfigurer(二)之namespace

阅读更多 12.6 指定加载顺序 有的时候我们可能需要或者是希望定义多个PropertyPlaceholderConfigurer,这个时候我们可以通过setOrder()方法来指定PropertyPlaceholderConfigurer的处...
阅读更多

12.6 指定加载顺序

有的时候我们可能需要或者是希望定义多个PropertyPlaceholderConfigurer,这个时候我们可以通过setOrder()方法来指定PropertyPlaceholderConfigurer的处理顺序,值越小的越先处理。这样就有两个问题要考虑,当一个属性变量可以被多个PropertyPlaceholderConfigurer进行替换时,先处理的将优先替换,替换后对于后处理的PropertyPlaceholderConfigurer来说对应的变量已经不存在了也就不能再进行替换了。第二个需要考虑的问题是当某些属性变量只能由某些个PropertyPlaceholderConfigurer进行替换时,使用默认设置的话在其前处理的PropertyPlaceholderConfigurer遇到不能替换的属性变量时将抛出异常,所以我们需要指定定义在前面的PropertyPlaceholderConfigurer忽略属性变量不能被解析的情况。

	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
		<property name="location" value="classpath:t1.properties"/> 
		<!-- 忽略变量不能被解析的情况 -->
		<property name="ignoreUnresolvablePlaceholders" value="true"/>
		<!-- 指定处理顺序 -->
		<property name="order" value="1"/>
	</bean>
	
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
		<property name="location" value="classpath:t2.properties"/> 
		<!-- 忽略变量不能被解析的情况 -->
		<property name="ignoreUnresolvablePlaceholders" value="true"/>
		<!-- 指定处理顺序 -->
		<property name="order" value="2"/>
	</bean>

12.7 使用命名空间进行定义

通过在Spring的配置文件中引入context命名空间,然后通过该命名空间的property-placeholder元素定义默认就可以在对应的bean容器中生成一个对应PropertySourcesPlaceholderConfigurer类型的对应的bean,在Spring3.1以前使用<context:property-placeholder/>将生成一个PropertyPlaceholderConfigurer类型的bean。可以通过该元素的location属性来指定对应的外部属性文件的位置,多个文件之间以逗号分隔。可以通过properties-ref属性来指定需要关联的内部属性定义对应的bean。可以通过local-override属性来指定当内部属性定义与外部属性文件定义存在相同的属性时是否内部属性定义需要覆盖外部属性文件定义的属性,默认为false。

可以通过system-properties-mode属性来指定针对系统属性的策略,针对不同的策略Spring底层会生成不同类型的bean进行处理,当指定其值为“ENVIRONMENT”时将生成PropertySourcesPlaceholderConfigurer类型的bean来进行对应变量的替换,其它三种类型将生成PropertyPlaceholderConfigurer类型的bean来进行对应变量的替换。

  • ENVIRONMENT表示将使用PropertySourcesPlaceholderConfigurer来处理对应的变量替换,其会根据当前的Spring Environment及一系列的PropertySources来替换对应的变量,这是默认值。这些PropertySources包括该通过该元素的location属性指定的外部属性文件和通过properties-ref属性指定的内部属性定义对应bean定义的属性。
  • NEVER表示从不使用系统属性来替换对应的变量。
  • FALLBACK表示只有在没有属性匹配当前变量的情况下才尝试使用系统属性来进行替换。
  • OVERRIDE表示将优先使用系统属性来替换对应的变量,即只有在系统属性不能替换对应变量时才使用外部文件定义的属性或是内部属性进行替换。

当location和properties-ref都没有进行指定时即表示只通过系统属性来进行变量的替换。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
	
	<context:property-placeholder
	location="classpath:t1.properties,classpath:t2.properties"
	properties-ref="innerProps" system-properties-mode="ENVIRONMENT" />
	
	<bean id="innerProps" class="java.util.Properties">
		<constructor-arg>
			<props>
				<prop key="p1">v1</prop>
				<prop key="p2">v2</prop>
			</props>
		</constructor-arg>
	</bean>

</beans>

还可以通过ignore-resource-not-found属性来指定是否忽略对应属性文件找不到的问题,默认为false,即会抛出异常。通过ignore-unresolvable属性指定是否忽略属性变量不能解析或者说是不能被替换的情况,默认为false,即不忽略,遇到不能解析的属性变量时将抛出异常。

	<context:property-placeholder
		location="classpath:t1.properties,classpath:t2.properties"
		properties-ref="innerProps" system-properties-mode="ENVIRONMENT"
		ignore-resource-not-found="false" ignore-unresolvable="false" />

还可以通过order属性指定当前BeanFactoryPostProcessor的加载顺序。

通过上面的介绍我们知道PropertySourcesPlaceholderConfigurer和PropertyPlaceholderConfigurer的作用其实是类似的,它们有着共同的父类PlaceholderConfigurerSupport。我们也可以单独的在Spring的配置文件中定义一个PropertySourcesPlaceholderConfigurer类型的bean用以替换对应的变量。与PropertyPlaceholderConfigurer相比,其除了不能指定systemPropertiesMode属性外,其它基本都和PropertyPlaceholderConfigurer是一样的。此外,如同它们的名称一样,PropertyPlaceholderConfigurer内部使用的是java.util.Properties作为属性资源的来源,而PropertySourcesPlaceholderConfigurer内部则是使用的org.springframework.core.env.PropertySources作为属性资源的来源。另外,PropertySourcesPlaceholderConfigurer还可以使用当前Spring的Environment对象getProperty()方法来获取属性变量对应的值以进行替换。更多信息请参看Spring的API文档。

(注:本文是基于Spring4.1.0所写)

本文转自:https://elim.iteye.com/blog/2387139

分享到:
评论

原文出处:https://liaoyang-777.iteye.com/blog/2436428

  • 发表于 2019-01-12 17:37
  • 阅读 ( 342 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除