Spring使用place holder spring
spring 使用PropertyPlaceholderConfigurer和PropertyOverrideConfigurer对象,which 实现了 ide
BeanFactoryPostProcessor接口,在spring的配置文件里面<beans>里面能够使用 ${properties_name}来引用在外 spa
部配置文件里面定义的变量。 code
好处是能够外部配置参数,在部署一些依赖环境的参数,好比JDBC链接,server ip等数据时比较方便在一个文件里面统一配置。 server
使用配置xml的方式 : xml
方式一: 对象
<context:property-placeholder location="classpath:jdbc.properties" />
<context:property-placeholder ignore-resource-not-found="true" ignore-unresolvable="true" system-properties-mode="OVERRIDE" location="classpath:db_config.properties"/>
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>db_config.properties</value> </list> </property> </bean> <bean name="jdbcTemplate" class="test.JdbcTemplate"> <property name="username" value="${db.username}"/> <property name="password" value="${db.password}"/> </bean>