现在的位置: 首页 > 综合 > 正文

Spring配置:用context:property-placeholder替换PropertyPlaceholderConfigurer

2013年12月18日 ⁄ 综合 ⁄ 共 843字 ⁄ 字号 评论关闭

有时候需要从properties文件中加载配置,以前的方式是这样的:

	<bean id="jdbcProperties" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations">
			<list>
				<value>classpath*:/spring/jdbc.properties</value>
			</list>
		</property>
	</bean>

最近发现这样也可以,代码更整洁:

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

在bean定义中依然可以通过“${}”这种方式来去值:

	<bean id="dataSource" 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}" />
		<property name="initialSize" value="${jdbc.initialSize}" />
		<property name="maxActive" value="${jdbc.maxActive}" />
		<property name="maxIdle" value="${jdbc.maxIdle}" />
		<property name="minIdle" value="${jdbc.minIdle}" />
	</bean>

抱歉!评论已关闭.