關於的一個有趣現象

先來看下A和B兩個模塊 


A模塊和B模塊都分別擁有本身的Spring XML配置,並分別擁有本身的配置文件: 

A模塊 

A模塊的Spring配置文件以下: 
mysql

Xml代碼  收藏代碼spring

  1. <?xml version="1.0" encoding="UTF-8" ?>  sql

  2. <beans xmlns="http://www.springframework.org/schema/beans"  app

  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  url

  4.        xmlns:context="http://www.springframework.org/schema/context"  spa

  5.        xmlns:p="http://www.springframework.org/schema/p"  code

  6.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd  xml

  7.        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">  get

  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這樣作是利大於弊的。

相關文章
相關標籤/搜索