Spring中PropertyPlaceholderConfigurer的使用

可以通过Spring中的PropertyPlaceholderConfigurer来读取配置信息:


1. Properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mysql
jdbc.username=root
jdbc.password=root
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:jdbc.properties"/>
</bean>

<bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
</bean>

Spring 2.5引进Context命名空间, 可以通过context:property-placeholder元素进行配置:

<context:property-placeholder location="classpath:jdbc.properties"/>

PropertyPlaceholderConfigurer不仅可以根据指定的Properties文件来查找属性,也可以通过java系统属性模式。 

spring 文档原文:



The PropertyPlaceholderConfigurer not only looks for properties in the Properties file you specify.
By default it also checks against the Java System properties if it cannot find a property in the specified properties files. 
You can customize this behavior by setting the systemPropertiesMode property of the configurer with one of the following three supported integer values:

  • never (0): Never check system properties

  • fallback (1): Check system properties if not resolvable in the specified properties files. This is the default.

  • override (2): Check system properties first, before trying the specified properties files. This allows system properties to override any other property source.

nerver(0): 不检查系统配置属性。

fallback(1): 检查系统配置属性,如果在指定属性文件中没有找到的话,使用系统配置属性,默认

override(2): 首先检查系统配置属性,系统配置属性优先。


<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
      <value>classpath:com/foo/strategy.properties</value>
  </property>
  <property name="properties">
      <value>custom.strategy.class=com.foo.DefaultStrategy</value>
  </property>
</bean>

<bean id="serviceStrategy" class="${custom.strategy.class}"/>

If the class cannot be resolved at runtime to a valid class, resolution of the bean fails when it is about to be created, which is during thepreInstantiateSingletons() phase of anApplicationContext for a non-lazy-init bean.


 上面这段话的意思是,如果配置信息中使用了系统配置属性的方式,那么属性中的类在加载的时候就应该是非延迟加载的bean。


Spring 3.1 开始使用org.springframework.context.support.PropertySourcesPlaceholderConfigurer。


		<!-- 方式一  -->
		<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
			<property name="locations">
				 <value>classpath:jdbc.properties</value>  
			</property>
		</bean>

		<!--方式二 -->
		<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
			<property name="location">
				 <value>classpath:jdbc.properties</value>  
			</property>
		</bean>

		<!-- 方式三 -->
		<context:property-placeholder location="classpath:jdbc.properties"/>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值