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>