<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 6  文章 - 129  trackbacks - 0
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(14)

    隨筆檔案(6)

    文章分類(467)

    文章檔案(423)

    相冊

    收藏夾(18)

    JAVA

    搜索

    •  

    積分與排名

    • 積分 - 825594
    • 排名 - 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 閱讀(7028) 評論(1)  編輯  收藏 所屬分類: spring

    FeedBack:
    # re: 使用外部屬性文件(關于PropertyPlaceholderConfigurer) [未登錄] 2008-06-19 16:46 小宋
    寫得不錯!  回復  更多評論
      
    主站蜘蛛池模板: 在线看片免费人成视频久网下载| 亚洲欧美第一成人网站7777| 一区二区3区免费视频| xvideos亚洲永久网址| gogo免费在线观看| 久久精品视频亚洲| 99无码人妻一区二区三区免费| 天堂亚洲国产中文在线| 国产一区在线观看免费| 国产高潮久久免费观看| 亚洲欧美在线x视频| 亚洲国产精品一区第二页| 欧洲一级毛片免费| 色妞www精品视频免费看| 久久精品国产亚洲一区二区| 亚洲高清无码专区视频| 日韩免费人妻AV无码专区蜜桃| 精品亚洲AV无码一区二区| 国产成人精品高清免费| 免费无码作爱视频| 亚洲精品天堂在线观看| 亚洲精品一卡2卡3卡三卡四卡| 无码欧精品亚洲日韩一区夜夜嗨 | 91福利视频免费| 理论亚洲区美一区二区三区| 亚洲欧洲日产国码久在线| 激情五月亚洲色图| 亚洲成AV人片天堂网无码| 亚洲欧洲日产国码无码网站| 最新中文字幕免费视频| 国产人成网在线播放VA免费| 国产亚洲精品bv在线观看| 亚洲xxxx视频| 亚洲综合激情五月色一区| 亚洲人成在线播放网站岛国| 又粗又大又长又爽免费视频 | 亚洲免费在线播放| 亚洲视频在线不卡| 亚洲女久久久噜噜噜熟女| 亚洲精品午夜国产VA久久成人| 在线免费观看国产视频|