国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

spring中propertyplaceholderconfigurer簡介

2019-11-14 21:04:07
字體:
來源:轉載
供稿:網友
sPRing中propertyplaceholderconfigurer簡介

Spring的框架中為您提供了一個 BeanFactoryPostProcessor 的實作類別: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer。藉由這個類別,您可以將一些組態設定,移出至.properties檔案中,如此的安排可以讓xml定義檔負責系統相關設定,而.properties檔可以作為客戶根據需求,自定義一些相關的參數。 來看一個Bean定義檔的實際例子: beans-config.xml

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="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">        <property name="location">            <value>hello.properties</value>        </property>    </bean>    <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">        <property name="helloWord">            <value>${onlyfun.caterpillar.helloWord}</value>        </property>    </bean></beans>

假設在helloBean中有許多依賴注入的屬性,這些都是比較不常變動的屬性,而其中helloWord會經常變動,可以透過hello.properties來簡單的設定,而這個資訊已設定在configBean的location屬性中: hello.properties

onlyfun.caterpillar.helloWord=Welcome!

在helloBean的helloWord屬性中,${onlyfun.caterpillar.helloWord}將會被hello.properties的helloWord所取代。 如果有多個.properties檔案,則可以透過locations屬性來設定,例如: beans-config.xml

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="configBean" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations">         <list>            <value>hello.properties</value>             <value>welcome.properties</value>             <value>other.properties</value>         </list>    </property>    </bean>     <bean id="helloBean" class="onlyfun.caterpillar.HelloBean">         <property name="helloWord">             <value>${onlyfun.caterpillar.helloWord}</value>         </property>           ...          </bean> </beans>

PropertyPlaceholderConfigurer類就是bean factory post-processor的一種,它的作用是一個資源屬性的配置器,能夠將BeanFactory的里定義的內容放在一個以.propertis后綴的文件中。 例如---spring-context.xml----

Xml代碼 :

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">     <property name="locations">         <list>            <value>/WEB-INF/jdbc.properties</value>        </list>     </property> </bean>
</bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">     <property name="driverClassName"><value>${driver}</value></property>     <property name="url"><value>jdbc:${dbname}</value></property> </bean>

 

而實際的jdbc.propertis文件是

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

而jdbc.propertis是怎樣引用的呢: 將上邊一段配置注冊在web.xml中就可以了

Xml代碼 :

<context-param>    <param-name>contextConfigLocation</param-name>     <param-value>/WEB-INF/spring-context.xml</param-value> </context-param> 當然,不要忘了spring的監聽器注冊  <listener>     <listener-class>         org.springframework.web.context.ContextLoaderListener      </listener-class> </listener>

這樣,一個簡單的數據源就設置完畢了。 實際上,PropertyPlaceholderConfigurer起的作用就是將占位符指向的數據庫配置信息放在bean中定義 的工具。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 山阳县| 荔浦县| 商河县| 靖西县| 洪湖市| 漳浦县| 盘锦市| 皋兰县| 会东县| 陈巴尔虎旗| 玉山县| 论坛| 云梦县| 昭平县| 丹东市| 柞水县| 颍上县| 双流县| 昆山市| 成都市| 乃东县| 长治市| 伊通| 和龙市| 樟树市| 景德镇市| 靖西县| 长泰县| 东丽区| 九龙县| 团风县| 大兴区| 普宁市| 赤壁市| 尼玛县| 临江市| 建始县| 吴江市| 家居| 天峻县| 洛南县|