Freud's Blog

Stay hungry, stay foolish. 少年辛苦终身事,莫向光阴惰寸功。

Spring 使用笔记之(三) - Placeholder

Posted on By Freud Kang

PlaceHolder

在Spring中,Placeholder的实现有2种方式,第一种是Context标签下的property-placeholder,第二种是PropertyPlaceholderConfigure的Bean配置。

Context标签下的property-placeholder

在配置文件中引入如下操作就可以在代码中使用诸如${jdbc.username}占位了。

xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"

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

PropertyPlaceholderConfigure

这种方式比较强大,支持从JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为。

<bean
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="ignoreResourceNotFound" value="true" />
	<property name="locations" value="classpath:/test.properties" />
</bean>

参考资料

http://blog.sina.com.cn/s/blog_4550f3ca0100ubmt.html