在spring-context.xml配置中,读取配置文件我们会这样配置,
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:conf-${spring.profiles.active}.properties</value>
</list>
</property>
<property name="fileEncoding" value="utf-8"/>
</bean>
殊不知,还有个容易被忽略的标签,<context:property-placeholder/>
为了简化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。
下面给出了配置示例,启用它后,开发者便不用配置PropertyPlaceholderConfigurer对象了。
<context:property-placeholder location="userinfo.properties"/>
它的功能非常丰富,如果它未找到${xxx}中定义的xxx键,它还会去JVM系统属性(System.getProperty())和环境变量(System.getenv())中寻找。
通过启用systemPropertiesMode和searchSystemEnvironment属性,开发者能够控制这一行为。