隨筆 - 6  文章 - 129  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(14)

          隨筆檔案(6)

          文章分類(467)

          文章檔案(423)

          相冊

          收藏夾(18)

          JAVA

          搜索

          •  

          積分與排名

          • 積分 - 827218
          • 排名 - 49

          最新評論

          閱讀排行榜

          評論排行榜

          <?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>classpath:com/starxing/test/jdbc.properties</value>
                  </property>
          <!--
           使用locations屬性定義多個配置文件
                 <property name="locations">
                      <list>
                          <value>classpath:config/maxid.properties</value>
                          <value>classpath:config/jdoserver.properties</value>
                      </list>
          </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必須手動運行:
                  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能夠自動辨認和應用在其上部署的實現了BeanFactoryPostProcessor的bean。這就意味著,當使用ApplicationContext的時候應用PropertyPlaceholderConfigurer會非常的方便。由于這個原因,建議想要使用這個或者其他bean
                  // factory postprocessor的用戶使用ApplicationContext代替BeanFactroy。
                  ApplicationContext context = new ClassPathXmlApplicationContext(
                          "com/starxing/test/conf.xml");
                  DriverManagerDataSource dataSource2 = (DriverManagerDataSource) context
                          .getBean("dataSource");
                  System.out.println(dataSource2.getDriverClassName());
              }

          }

          相關文檔:


           使用這一解決方案,我們可以生成如下的屬性文件(/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>

            如上所述,我們定義了一個PropertyPlaceholderConfigurer類的實例,并將其位置屬性設置為我們的屬性文件。該類被實現為Bean工廠的后處理器,并將使用定義在文件中的屬性來代替所有的占位符(${...}value)。

            利用這種技術,我們可以從applicationContext.xml中移除所有特定于主機的配置屬性。通過這種方式,我們可以自由地為該文件添加新的Bean,而不必擔心特定于主機屬性的同步性。這樣可以簡化生產部署和維護。




          PropertyPlaceholderConfigurer作為一個bean factory post-processor實現,可以用來將BeanFactory定義中的屬性值放置到另一個單獨的Java Properties格式的文件中。這使得用戶不用對BeanFactory的主XML定義文件進行復雜和危險的修改,就可以定制一些基本的屬性(比如說數據庫的urls,用戶名和密碼)。

          考慮一個BeanFactory定義的片斷,里面用占位符定義了DataSource:

          在下面這個例子中,定義了一個datasource,并且我們會在一個外部Porperties文件中配置一些相關屬性。 在運行時,我們為BeanFactory提供一個PropertyPlaceholderConfigurer,它將用Properties文件中的值替換掉這個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>

          真正的值來自于另一個Properties格式的文件:

          jdbc.driverClassName=org.hsqldb.jdbcDriver
          jdbc.url=jdbc:hsqldb:hsql://production:9002
          jdbc.username=sa
          jdbc.password=root

          如果要在BeanFactory中使用,bean factory post-processor必須手動運行:

          XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
          PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
          cfg.setLocation(new FileSystemResource("jdbc.properties"));
          cfg.postProcessBeanFactory(factory);

          注意,ApplicationContext能夠自動辨認和應用在其上部署的實現了BeanFactoryPostProcessor的bean。這就意味著,當使用ApplicationContext的時候應用PropertyPlaceholderConfigurer會非常的方便。由于這個原因,建議想要使用這個或者其他bean factory postprocessor的用戶使用ApplicationContext代替BeanFactroy。

          PropertyPlaceHolderConfigurer不僅僅在你指定的Porperties文件中查找屬性, 如果它在其中沒有找到你想使用的屬性,它還會在Java的系統properties中查找。 這個行為能夠通過設置配置中的systemPropertiesMode 屬性來定制。這個屬性有三個值, 一個讓配置總是覆蓋,一個讓它永不覆蓋,一個讓它僅在properties文件中找不到的時候覆蓋。 請參考 PropertiesPlaceholderConfigurer的JavaDoc獲得更多信息。
          文章來源:http://www.cublog.cn/u/9295/showart.php?id=261437



          posted on 2007-12-23 22:23 Ke 閱讀(7033) 評論(1)  編輯  收藏 所屬分類: spring

          FeedBack:
          # re: 使用外部屬性文件(關于PropertyPlaceholderConfigurer) [未登錄] 2008-06-19 16:46 小宋
          寫得不錯!  回復  更多評論
            
          主站蜘蛛池模板: 盐山县| 皋兰县| 舞阳县| 诸城市| 大宁县| 镶黄旗| 吉林市| 囊谦县| 三江| 玉树县| 康保县| 广安市| 尼勒克县| 东乌珠穆沁旗| 行唐县| 栾城县| 左权县| 临泉县| 福安市| 泰宁县| 平泉县| 陇川县| 景洪市| 宜丰县| 昌图县| 长垣县| 邳州市| 加查县| 桐柏县| 霍城县| 建始县| 务川| 卓尼县| 砀山县| 日喀则市| 大同市| 来凤县| 甘泉县| 平谷区| 静海县| 丰镇市|