1. jdbc.properties
database.url=jdbc:mysql://localhost/smaple
database.driver=org.gjt.mm.mysql.Driver
database.user=root
database.password=star1xing
2.conf.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>com/starxing/test/jdbc.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url">
<value>${database.url}</value>
</property>
<property name="driverClassName">
<value>${database.driver}</value>
</property>
<property name="username">
<value>${database.user}</value>
</property>
<property name="password">
<value>${database.password}</value>
</property>
</bean>
</beans>
3.Config.java
package com.starxing.test;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
public class Config {
public static void main(String[] args) {
XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource(
"com/starxing/test/conf.xml"));
// 如果要在BeanFactory中使用,bean factory post-processor必須手動(dòng)運(yùn)行:
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource(
"com/starxing/test/jdbc.properties"));
cfg.postProcessBeanFactory(factory);
DriverManagerDataSource dataSource = (DriverManagerDataSource) factory
.getBean("dataSource");
System.out.println(dataSource.getDriverClassName());
// 注意,ApplicationContext能夠自動(dòng)辨認(rèn)和應(yīng)用在其上部署的實(shí)現(xiàn)了BeanFactoryPostProcessor的bean。這就意味著,當(dāng)使用ApplicationContext的時(shí)候應(yīng)用PropertyPlaceholderConfigurer會(huì)非常的方便。由于這個(gè)原因,建議想要使用這個(gè)或者其他bean
// factory postprocessor的用戶(hù)使用ApplicationContext代替BeanFactroy。
ApplicationContext context = new ClassPathXmlApplicationContext(
"com/starxing/test/conf.xml");
DriverManagerDataSource dataSource2 = (DriverManagerDataSource) context
.getBean("dataSource");
System.out.println(dataSource2.getDriverClassName());
}
}
相關(guān)文檔:
使用這一解決方案,我們可以生成如下的屬性文件(/WEB-INF/jdbc.properties):
jdbc.driver=org.postgresql.Driver
jdbc.url=jdbc:postgresql://localhost/test
jdbc.user=postgres
jdbc.password=
我們的Bean配置如下:
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>/WEB-INF/jdbc.properties</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driver}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.user}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean>
如上所述,我們定義了一個(gè)PropertyPlaceholderConfigurer類(lèi)的實(shí)例,并將其位置屬性設(shè)置為我們的屬性文件。該類(lèi)被實(shí)現(xiàn)為Bean工廠的后處理器,并將使用定義在文件中的屬性來(lái)代替所有的占位符(${...}value)。
利用這種技術(shù),我們可以從applicationContext.xml中移除所有特定于主機(jī)的配置屬性。通過(guò)這種方式,我們可以自由地為該文件添加新的Bean,而不必?fù)?dān)心特定于主機(jī)屬性的同步性。這樣可以簡(jiǎn)化生產(chǎn)部署和維護(hù)。
PropertyPlaceholderConfigurer作為一個(gè)bean factory post-processor實(shí)現(xiàn),可以用來(lái)將BeanFactory定義中的屬性值放置到另一個(gè)單獨(dú)的Java Properties格式的文件中。這使得用戶(hù)不用對(duì)BeanFactory的主XML定義文件進(jìn)行復(fù)雜和危險(xiǎn)的修改,就可以定制一些基本的屬性(比如說(shuō)數(shù)據(jù)庫(kù)的urls,用戶(hù)名和密碼)。
考慮一個(gè)BeanFactory定義的片斷,里面用占位符定義了DataSource:
在下面這個(gè)例子中,定義了一個(gè)datasource,并且我們會(huì)在一個(gè)外部Porperties文件中配置一些相關(guān)屬性。 在運(yùn)行時(shí),我們?yōu)锽eanFactory提供一個(gè)PropertyPlaceholderConfigurer,它將用Properties文件中的值替換掉這個(gè)datasource的屬性值:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
<property name="url"><value>${jdbc.url}</value></property>
<property name="username"><value>${jdbc.username}</value></property>
<property name="password"><value>${jdbc.password}</value></property>
</bean>
真正的值來(lái)自于另一個(gè)Properties格式的文件:
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root
如果要在BeanFactory中使用,bean factory post-processor必須手動(dòng)運(yùn)行:
XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
cfg.setLocation(new FileSystemResource("jdbc.properties"));
cfg.postProcessBeanFactory(factory);
注意,ApplicationContext能夠自動(dòng)辨認(rèn)和應(yīng)用在其上部署的實(shí)現(xiàn)了BeanFactoryPostProcessor的bean。這就意味著,當(dāng)使用ApplicationContext的時(shí)候應(yīng)用PropertyPlaceholderConfigurer會(huì)非常的方便。由于這個(gè)原因,建議想要使用這個(gè)或者其他bean factory postprocessor的用戶(hù)使用ApplicationContext代替BeanFactroy。
PropertyPlaceHolderConfigurer不僅僅在你指定的Porperties文件中查找屬性, 如果它在其中沒(méi)有找到你想使用的屬性,它還會(huì)在Java的系統(tǒng)properties中查找。 這個(gè)行為能夠通過(guò)設(shè)置配置中的systemPropertiesMode 屬性來(lái)定制。這個(gè)屬性有三個(gè)值, 一個(gè)讓配置總是覆蓋,一個(gè)讓它永不覆蓋,一個(gè)讓它僅在properties文件中找不到的時(shí)候覆蓋。 請(qǐng)參考 PropertiesPlaceholderConfigurer的JavaDoc獲得更多信息。
來(lái)自:http://blog.chinaunix.net/u/9295/showart.php?id=261437