欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

spring 引用 多個 屬性文件

系統 2113 0

先來看下A和B兩個模塊


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 ??



問題來了

單獨運行A模塊,或單獨運行B模塊都是正常的,但將A和B兩個模塊集成后運行,Spring容器就啟動不了了:

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




到底出了啥問題

隨便搜索了一下,還發現很多人遇到這個問題,這個就是來自stackoverflow的問題:
http://stackoverflow.com/questions/7940452/spring-application-context-not-able-to-load-property-placeholder-properties

可惜啊,好像都沒有人給出正確的解決。

那究竟是什么問題呢?也想了很久哦....終于回想起來了(寫書時讀過Spring源碼),原來是Spring容器采用反射掃描的發現機制,在探 測到Spring容器中有一個 org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的 Bean就會停止對剩余PropertyPlaceholderConfigurer的掃描(Spring 3.1已經使用PropertySourcesPlaceholderConfigurer替代 PropertyPlaceholderConfigurer了)。

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

拿上來的例子來說,如果A和B模塊是單獨運行的,由于Spring容器都只有一個PropertyPlaceholderConfigurer, 因此屬性文件會被正常加載并替換掉。如果A和B兩模塊集成后運行,Spring容器中就有兩個 PropertyPlaceholderConfigurer Bean了,這時就看誰先誰后了, 先的保留,后的忽略!因此,只加載到了一個屬性文件,因而造成無法正確進行屬性替換的問題。

咋解決呢?

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

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 > ??



進一步思考

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

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

?

注意:如果有父子容器,如web應用,則應該各自配置一個屬性文件,這樣不會有問題,最終結論,每個spring容器只能有一個 PropertyPlaceholderConfigurer。

spring 引用 多個 屬性文件


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 偷拍亚洲制服另类无码专区 | 中文字幕在线免费看 | 欧美人两个人激情的免费视频 | 91免费国产在线 | 国产精品免费入口视频 | 欧美精品一区三区 | 久久久一区二区三区精品 | 精品成人久久久 | 久久只有这才是精品99 | 日本久草视频 | 日韩欧美国产中文 | 国产毛片片精品天天看视频 | 激情小说激情图片激情电影 | 天天插天天干 | 国产精品999 | 国产婷婷精品av在线 | 东京不太热在线新视频 | 欧美精| 超97在线观看精品国产 | 天天射天天操天天干 | 午夜伦理在线播放 | 久久久久久久99精品免费观看 | 日本香蕉一区二区三区 | 在线播放一区二区三区 | 色婷婷在线播放 | 欧美成人精品欧美一级乱黄 | 欧美成人免费午夜影视 | 亚洲激情一区二区 | 人人干操 | 性欧美26uuu在线观看 | 九九综合九九综合 | 久久久久久久久淑女av国产精品 | 久久综合九色综合欧洲 | 欧美精品一区二区在线观看 | 日韩av免费在线观看 | 91婷婷色| 一区二区三区中文字幕 | 久久com | 精品国产一区二区三区成人影院 | 国产精品久久久久久久久免费 | 国产乱码精品一区二区三区五月婷 |