亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

spring 引用 多個(gè) 屬性文件

系統(tǒng) 2325 0

先來(lái)看下A和B兩個(gè)模塊


A模塊和B模塊都分別擁有自己的Spring XML配置,并分別擁有自己的配置文件:

A模塊

A模塊的Spring配置文件如下:

Xml代碼 ? 收藏代碼
  1. <? xml ? version = "1.0" ? encoding = "UTF-8" ? ?> ??
  2. < beans ? xmlns = "http://www.springframework.org/schema/beans" ??
  3. ??????? xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" ??
  4. ??????? xmlns:context = "http://www.springframework.org/schema/context" ??
  5. ??????? xmlns:p = "http://www.springframework.org/schema/p" ??
  6. ??????? xsi:schemaLocation ="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.2.xsd??
  7. ???????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.2.xsd" > ??
  8. ??? < context:property-placeholder ? location = "classpath*:conf/conf_a.properties" /> ??
  9. ??? < bean ? class = "com.xxx.aaa.Bean1" ??
  10. ?????????? p:driverClassName = "${modulea.jdbc.driverClassName}" ??
  11. ?????????? p:url = "${modulea.jdbc.url}" ??
  12. ?????????? p:username = "${modulea.jdbc.username}" ??
  13. ?????????? p:password = "${modulea.jdbc.password}" /> ??
  14. </ beans > ??


其配置文件位于類路徑conf/conf_a.properties中:

Xml代碼 ? 收藏代碼
  1. modulea.jdbc.driverClassName = com .mysql.jdbc.Driver??
  2. modulea.jdbc.username = cartan ??
  3. modulea.jdbc.password = superman ??
  4. modulea.jdbc.url =jdbc:mysql://127.0.0.1:3306/modulea? useUnicode = true & characterEncoding = utf8 ??



B模塊

B模塊的Spring配置文件如下:

Xml代碼 ? 收藏代碼
  1. <? xml ? version = "1.0" ? encoding = "UTF-8" ? ?> ??
  2. < beans ? xmlns = "http://www.springframework.org/schema/beans" ??
  3. ??????? xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" ??
  4. ??????? xmlns:context = "http://www.springframework.org/schema/context" ??
  5. ??????? xmlns:p = "http://www.springframework.org/schema/p" ??
  6. ??????? xsi:schemaLocation ="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.2.xsd??
  7. ???????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.2.xsd" > ??
  8. ??? < context:property-placeholder ? location = "classpath*:conf/conf_b.properties" /> ??
  9. ??? < bean ? class = "com.xxx.bbb.Bean1" ??
  10. ?????????? p:driverClassName = "${moduleb.jdbc.driverClassName}" ??
  11. ?????????? p:url = "${moduleb.jdbc.url}" ??
  12. ?????????? p:username = "${moduleb.jdbc.username}" ??
  13. ?????????? p:password = "${moduleb.jdbc.password}" /> ??
  14. </ beans > ??


其配置文件位于類路徑conf/conf_b.properties中:

Java代碼 ? 收藏代碼
  1. moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver??
  2. moduleb.jdbc.username=cartan??
  3. moduleb.jdbc.password=superman??
  4. moduleb.jdbc.url=jdbc:mysql: //127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8 ??



問(wèn)題來(lái)了

單獨(dú)運(yùn)行A模塊,或單獨(dú)運(yùn)行B模塊都是正常的,但將A和B兩個(gè)模塊集成后運(yùn)行,Spring容器就啟動(dòng)不了了:

引用
Could not resolve placeholder 'moduleb.jdbc.driverClassName' in string value "${moduleb.jdbc.driverClassName}"




到底出了啥問(wèn)題

隨便搜索了一下,還發(fā)現(xiàn)很多人遇到這個(gè)問(wèn)題,這個(gè)就是來(lái)自stackoverflow的問(wèn)題:
http://stackoverflow.com/questions/7940452/spring-application-context-not-able-to-load-property-placeholder-properties

可惜啊,好像都沒(méi)有人給出正確的解決。

那究竟是什么問(wèn)題呢?也想了很久哦....終于回想起來(lái)了(寫書時(shí)讀過(guò)Spring源碼),原來(lái)是Spring容器采用反射掃描的發(fā)現(xiàn)機(jī)制,在探 測(cè)到Spring容器中有一個(gè) org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的 Bean就會(huì)停止對(duì)剩余PropertyPlaceholderConfigurer的掃描(Spring 3.1已經(jīng)使用PropertySourcesPlaceholderConfigurer替代 PropertyPlaceholderConfigurer了)。

而<context:property-placeholder/>這個(gè)基于命名空間的配置,其實(shí)內(nèi)部就是創(chuàng)建一個(gè)PropertyPlaceholderConfigurer Bean而已。 換句話說(shuō),即Spring容器僅允許最多定義一個(gè)PropertyPlaceholderConfigurer(或<context:property-placeholder/>),其余的會(huì)被Spring忽略掉 (其實(shí)Spring如果提供一個(gè)警告就好了)。

拿上來(lái)的例子來(lái)說(shuō),如果A和B模塊是單獨(dú)運(yùn)行的,由于Spring容器都只有一個(gè)PropertyPlaceholderConfigurer, 因此屬性文件會(huì)被正常加載并替換掉。如果A和B兩模塊集成后運(yùn)行,Spring容器中就有兩個(gè) PropertyPlaceholderConfigurer Bean了,這時(shí)就看誰(shuí)先誰(shuí)后了, 先的保留,后的忽略!因此,只加載到了一個(gè)屬性文件,因而造成無(wú)法正確進(jìn)行屬性替換的問(wèn)題。

咋解決呢?

定位問(wèn)題需要9999元錢,解決問(wèn)題只需要1元錢 。
屬性文件加載在統(tǒng)一的地方做,不要分模塊加載即可。

A模塊a.xml:

Xml代碼 ? 收藏代碼
  1. <? xml ? version = "1.0" ? encoding = "UTF-8" ? ?> ??
  2. < beans ? xmlns = "http://www.springframework.org/schema/beans" ??
  3. ??????? xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" ??
  4. ??????? xmlns:context = "http://www.springframework.org/schema/context" ??
  5. ??????? xmlns:p = "http://www.springframework.org/schema/p" ??
  6. ??????? xsi:schemaLocation ="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.2.xsd??
  7. ???????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.2.xsd" > ??
  8. ??? <!--<context:property-placeholder?location="classpath*:conf/conf_a.properties"/>--> ??
  9. ??? < bean ? class = "com.xxx.aaa.Bean1" ??
  10. ?????????? p:driverClassName = "${modulea.jdbc.driverClassName}" ??
  11. ?????????? p:url = "${modulea.jdbc.url}" ??
  12. ?????????? p:username = "${modulea.jdbc.username}" ??
  13. ?????????? p:password = "${modulea.jdbc.password}" /> ??
  14. </ beans > ??



B模塊b.xml:

Xml代碼 ? 收藏代碼
  1. <? xml ? version = "1.0" ? encoding = "UTF-8" ? ?> ??
  2. < beans ? xmlns = "http://www.springframework.org/schema/beans" ??
  3. ??????? xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" ??
  4. ??????? xmlns:context = "http://www.springframework.org/schema/context" ??
  5. ??????? xmlns:p = "http://www.springframework.org/schema/p" ??
  6. ??????? xsi:schemaLocation ="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.2.xsd??
  7. ???????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.2.xsd" > ??
  8. ??? <!--<context:property-placeholder?location="classpath*:conf/conf_b.properties"/>--> ??
  9. ??? < bean ? class = "com.xxx.bbb.Bean1" ??
  10. ?????????? p:driverClassName = "${moduleb.jdbc.driverClassName}" ??
  11. ?????????? p:url = "${moduleb.jdbc.url}" ??
  12. ?????????? p:username = "${moduleb.jdbc.username}" ??
  13. ?????????? p:password = "${moduleb.jdbc.password}" /> ??
  14. </ beans > ??



集成:

Xml代碼 ? 收藏代碼
  1. <? xml ? version = "1.0" ? encoding = "UTF-8" ? ?> ??
  2. < beans ? xmlns = "http://www.springframework.org/schema/beans" ??
  3. ??????? xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" ??
  4. ??????? xmlns:context = "http://www.springframework.org/schema/context" ??
  5. ??????? xmlns:p = "http://www.springframework.org/schema/p" ??
  6. ??????? xsi:schemaLocation ="http://www.springframework.org/schema/beans?http://www.springframework.org/schema/beans/spring-beans-3.2.xsd??
  7. ???????http://www.springframework.org/schema/context?http://www.springframework.org/schema/context/spring-context-3.2.xsd" > ??
  8. ??? < context:property-placeholder ? location = "classpath*:conf/conf*.properties" /> ??
  9. ??? < import ? resource = "a.xml" /> ??
  10. ??? < import ? resource = "b.xml" /> ??
  11. </ beans > ??



進(jìn)一步思考

為什么啊?Spring為什么要這樣呢?細(xì)想想是有道理的,一個(gè)項(xiàng)目或一個(gè)系統(tǒng)的配置應(yīng)該放在一起,不宜分散。
這樣才可以做到統(tǒng)一管控,否則到處都有配置,到底是加載哪個(gè)配置文件呢?有時(shí)你還會(huì)不小心讓JAR中的Spring配置文件加載一個(gè)位于JAR中 的屬性文件,而外面有更改不了。如果Spring使用了這種機(jī)制,即使JAR包中的Spring配置文件使用<context:property- placeholder/>引用到JAR中的屬性文件,只要你要外而的Spring配置文件中顯示提供一 個(gè)<context:property-placeholder/>指定另一個(gè)屬性文件 ,就可以覆蓋JAR中的默認(rèn)配置了。

想了一想,Spring這樣做是利大于弊的。

?

注意:如果有父子容器,如web應(yīng)用,則應(yīng)該各自配置一個(gè)屬性文件,這樣不會(huì)有問(wèn)題,最終結(jié)論,每個(gè)spring容器只能有一個(gè) PropertyPlaceholderConfigurer。

spring 引用 多個(gè) 屬性文件


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦?。?!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 国语精品91自产拍在线观看二区 | 特黄日韩免费一区二区三区 | 深夜国产 | 国产成a人亚洲精v品久久网 | 欧美亚洲国产精品久久久久 | 亚洲国产成人久久综合一区77 | aaaaa级毛片 aaaa级毛片欧美的 | 玖玖在线资源 | 青青影院一区二区免费视频 | 亚洲综合精品一二三区在线 | 久久精品久久久久久久久人 | 精品福利在线 | 亚州成人 | 亚洲视频在线观看视频 | 久久是精品| 中国毛片免费看 | 亚洲国产欧美另类 | 亚洲精品丝袜在线一区波多野结衣 | 国产午夜精品一区二区 | 中文字幕久精品免费视频蜜桃视频 | 亚洲国产精品区 | 狠狠色丁香久久综合五月 | 久久久久女人精品毛片九一 | 天天操操操操 | 久久女同互慰一区二区三区 | 精产网红自拍在线 | 四虎hu| 福利影院在线 | 99久久国产综合精品女小说 | 久久视频精品36线视频在线观看 | 国产香蕉75在线播放 | 国产精品视频2021 | 国产色吧| 四虎永久在线精品视频播放 | 亚洲一区中文字幕在线观看 | 国产精品久久久久久久久久久久 | 深夜免费在线观看 | 五月婷婷激情综合网 | 伦理一区 | 一本大道加勒比久久综合 | 欧美精品亚洲网站 |