多個spring配置xml文件 及 properties文件

最近發(fā)現(xiàn)不能多個context:property-placeholder來注入properties文件:
項目Common (通用項目)
config.properties
spring.xml (注入config.properties文件, 通過${xxxx}來注入bean屬性 )

項目SpringData(數(shù)據(jù)層)
database.properties
springdata.xml (注入database.properties文件,通過${xxxx}來注入bean 屬性)

項目Web (引用SpringData與Common項目)
web.properties
spring-mvc.xml ( 引用Common項目的spring.xml 和SpringData項目的springdata.xml )

<pre>
<import location="classpath:spring.xml" />
<import location="classpath
:springdata.xml" />
</pre>

問題:
加載Web項目時會提示Could not resolve placeholder 'xxx.yyy' in string value "${xxx.yyy}"
查看日志,沒有發(fā)現(xiàn)應(yīng)該要加載properties文件被加載,而只加載了common項目下的config.properties文件

按照Web項目配置文件spring-mvc.xml
先import Common項目spring.xml文件
后import SpringData項目springdata.xml文件
的順序,只加載了config.properties文件,后面的都被忽略掉。

找了一下資料:
http://www.iteye.com/topic/1131688
https://segmentfault.com/q/1010000006823872

發(fā)現(xiàn)第一個第二個properties文件(Common/spring.xml和SpringData/database.properties)
需要設(shè)置ignore-unresolved=true,第三個文件(Web/config.properites)才會被加載,這個ignore-unresolved屬性就好像html dom模型里的冒泡事件一樣,每觸發(fā)一次事件都由這個事件來決定事件消息是否冒泡傳遞到上一級。

但這種方式有可能出現(xiàn)加載順序混亂的情況,所以使用以下方式最好:

Common/Spring.xml 不再注入config.properties文件
SpringData/SpringData.xml 不再注入database.properties文件
所有的properties加載由web/spring-mvc.xml 來統(tǒng)一注入:

<pre>
<context:property-placeholder
location="classpath:config.properties,classpath:database.properties,classpath:web.properties" />
<import location="classpath:spring.xml" />
<import location="classpath
:springdata.xml" />
</pre>

這樣就很好的告訴給spring容器應(yīng)該加載什么文件,以及加載順序。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容