1. 程式人生 > >applicationContext.xml裡org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

applicationContext.xml裡org.springframework.beans.factory.config.PropertyPlaceholderConfigurer

1.先貼程式碼

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

2.解釋

PropertyPlaceholderConfigurer是個bean工廠後置處理器的實現,也就是 BeanFactoryPostProcessor介面的一個實現。PropertyPlaceholderConfigurer可以將上下文(配置文 件)中的屬性值放在另一個單獨的標準java Properties檔案中去。在XML檔案中用${key}替換指定的properties檔案中的值。這樣的話,只需要對properties檔案進 行修改,而不用對xml配置檔案進行修改。

3.升級

為簡化PropertyPlaceholderConfigurer的使用,Spring提供了<context:property-placeholder/>元素。下面給出了配置示例,啟用它後,開發者便不用配置PropertyPlaceholderConfigurer物件了。

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