国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Spring 使用Properties配置文件
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必須手動運行:
        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能夠自動辨認和應用在其上部署的實現(xiàn)了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類的實例,并將其位置屬性設置為我們的屬性文件。該類被實現(xiàn)為Bean工廠的后處理器,并將使用定義在文件中的屬性來代替所有的占位符(${...}value)。

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




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

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

在下面這個例子中,定義了一個datasource,并且我們會在一個外部Porperties文件中配置一些相關屬性。 在運行時,我們?yōu)锽eanFactory提供一個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能夠自動辨認和應用在其上部署的實現(xiàn)了BeanFactoryPostProcessor的bean。這就意味著,當使用ApplicationContext的時候應用PropertyPlaceholderConfigurer會非常的方便。由于這個原因,建議想要使用這個或者其他bean factory postprocessor的用戶使用ApplicationContext代替BeanFactroy。

PropertyPlaceHolderConfigurer不僅僅在你指定的Porperties文件中查找屬性, 如果它在其中沒有找到你想使用的屬性,它還會在Java的系統(tǒng)properties中查找。 這個行為能夠通過設置配置中的systemPropertiesMode 屬性來定制。這個屬性有三個值, 一個讓配置總是覆蓋,一個讓它永不覆蓋,一個讓它僅在properties文件中找不到的時候覆蓋。 請參考 PropertiesPlaceholderConfigurer的JavaDoc獲得更多信息。

本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
PropertyPlaceholderConfigurer的用法講解
淺析Spring框架下PropertyPlaceholderConfigurer類
spring 配置文件管理
容器擴展點之PropertyPlaceholderConfigurer【整理】
關于系統(tǒng)中使用多個PropertyPlaceholderConfigurer的配置
Spring常用的接口和類
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服