SpringMVC中如何讀取配置檔案中的值
第一種情況是在測試類中讀取配置檔案中的值,實現如下:
1、在spring-mybatis配置檔案中引入配置檔案,程式碼如下:
<span style="font-family:Comic Sans MS;"> <!-- 引入配置檔案 --> <bean id="prpertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:jdbc.properties</value> </list> </property> </bean></span>
2、為了讓controller讀取到配置檔案,仍需要在spring.xml檔案中引入配置檔案
<span style="font-family:Comic Sans MS;"><context:property-placeholder location="classpath:jdbc.properties" /></span>
3、在測試類中寫成如下形式:
<span style="font-family:Comic Sans MS;">@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations={"classpath:spring-mybatis.xml","classpath:spring.xml"}) public class TestMyBatis{ //這是對應配置檔案中的變數,給變數注入值 @Value("${picUrl}") private String picUrl; 。。。。。。</span>
這樣便自動將配置檔案中picUrl變數的值注入給了picUrl,可以測試列印輸出其值,發現就是配置檔案中的值。
第二種情況是在Controller中讀取配置檔案中的值
之前我寫了兩個配置檔案,想同時載入,但是發現一直出現各種問題。原來在spring.xml檔案中寫兩個這樣的:
這樣是行不通的。<span style="font-family:Comic Sans MS;"> <context:property-placeholder location="classpath:jdbc.properties" /> <context:property-placeholder location="classpath:picPath.properties"/></span>
後來我將這兩個配置檔案的內容合併,儲存在了jdbc.properties中,便可以在Controller利用@Value對屬性值進行注入了。
看看我的Controller類的寫法:
<span style="font-family:Comic Sans MS;">@Controller
@Configuration
@ImportResource("classpath:spring.xml")
@RequestMapping("/photo")
public class FileUploadController {
@RequestMapping(value="/tofile")
public String toFileUpLoad(HttpServletRequest request,Model model){
return "fileUpLoad";
}
//@Value("#{settings['picPath.picUrl']}")
@Value("${picUrl}")
private String picUrl;
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}</span>
注意一定要引入配置檔案,在類名上加上,如下兩個註解,不然給picUrl變數注入的值不是配置檔案中的值,而是字串“${picUrl}”
<span style="font-family:Comic Sans MS;"><strong>@Configuration
@ImportResource("classpath:spring.xml")</strong></span>
下面來分析下之前分散到兩個配置檔案中載入不成功的原因:
比如有A和B兩個模組
A模組和B模組都分別擁有自己的Spring XML配置,並分別擁有自己的配置檔案
A模組:
A模組的spring配置檔案如下:
<span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath*:conf/conf_a.properties"/>
<bean class="com.xxx.<span style="color:#cc0000;">aaa.Bean1</span>"
p:driverClassName="${modulea.jdbc.driverClassName}"
p:url="${modulea.jdbc.url}"
p:username="${modulea.jdbc.username}"
p:password="${modulea.jdbc.password}"/>
</beans> </span>
其配置檔案位於類路徑conf/conf_a.properties中:
<span style="font-family:Comic Sans MS;">modulea.jdbc.driverClassName=com.mysql.jdbc.Driver
modulea.jdbc.username=cartan
modulea.jdbc.password=superman
modulea.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8</span>
B模組:
B模組的Spring配置檔案如下:
<span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath*:conf/conf_b.properties"/>
<bean class="com.xxx.<span style="color:#cc0000;">bbb.Bean1</span>"
p:driverClassName="${moduleb.jdbc.driverClassName}"
p:url="${moduleb.jdbc.url}"
p:username="${moduleb.jdbc.username}"
p:password="${moduleb.jdbc.password}"/>
</beans> </span>
其配置檔案位於類路徑conf/conf_b.properties中:
<span style="font-family:Comic Sans MS;">moduleb.jdbc.driverClassName=com.mysql.jdbc.Driver
moduleb.jdbc.username=cartan
moduleb.jdbc.password=superman
moduleb.jdbc.url=jdbc:mysql://127.0.0.1:3306/modulea?useUnicode=true&characterEncoding=utf8</span>
問題出現了,單獨執行A模組或B模組都是正常,但是將A和B兩個模組整合後執行,Spring容器就啟動不了:
出現的問題:Could not resolve placeholder 'moduleb.jdbc.driverClassName' in string value "${moduleb.jdbc.driverClassName}"
問題的原因是:Spring容器採用反射掃描的發現機制,在探測到Spring容器中有一個org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean
就會停止對剩餘PropertyPlaceholderConfigurer的掃描(Spring3.1已經使用PropertySourcesPlaceholderConfigurer替代
PropertyPlaceholderConfigurer了)。
而<context:property-placeholder/>這個基於名稱空間的配置,其實內部就是建立一個PropertyPlaceholderConfigurer Bean而已。換句話說,
即Spring容器僅允許最多定義一個PropertyPlaceholderConfigurer(或<context:property-placeholder),其餘的會被Spring忽略掉。
拿上來的例子來說,如果A和B模組是單獨執行的,由於Spring容器都只有一個PropertyPlaceholderConfigurer,因此屬性檔案會被正常載入並替換掉。如果A和B兩模組整合後執行,Spring容器中就有兩個PropertyPlaceholderConfigurer Bean了,這時就看誰先誰後了,
先的保留,後的忽略!因此,只加載到了一個屬性檔案,因而造成無法正確進行屬性替換的問題。
解決辦法:
屬性檔案載入在統一的地方做,不需要分模組載入
A模組a.xml:
<span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--<context:property-placeholder location="classpath*:conf/conf_a.properties"/>-->
<bean class="com.xxx.<strong><span style="color:#cc0000;">aaa.Bean1</span></strong>"
p:driverClassName="${modulea.jdbc.driverClassName}"
p:url="${modulea.jdbc.url}"
p:username="${modulea.jdbc.username}"
p:password="${modulea.jdbc.password}"/>
</beans> </span>
B模組b.xml:
<span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<!--<context:property-placeholder location="classpath*:conf/conf_b.properties"/>-->
<bean class="com.xxx.<strong><span style="color:#ff0000;">bbb.Bean1</span></strong>"
p:driverClassName="${moduleb.jdbc.driverClassName}"
p:url="${moduleb.jdbc.url}"
p:username="${moduleb.jdbc.username}"
p:password="${moduleb.jdbc.password}"/>
</beans></span>
整合:
<span style="font-family:Comic Sans MS;"><?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:property-placeholder location="classpath*:conf/conf*.properties"/>
<import resource="a.xml"/>
<import resource="b.xml"/>
</beans> </span>
Spring為什麼要這樣呢?細想想是有道理的,一個專案或一個系統的配置應該放在一起,不宜分散。
這樣才可以做到統一管控,否則到處都有配置,到底是載入哪個配置檔案呢?有時你還會不小心讓JAR中的Spring配置檔案載入一個位於JAR中的屬性檔案,
而外面又更改不了。如果Spring使用了這種機制,即使JAR包中的Spring配置檔案使用<context:property-placeholder/>引用到JAR中的屬性檔案,只要Spring配置檔案中顯示提供一個<context:property-placeholder/>指定另一個屬性檔案 ,就可以覆蓋JAR中的預設配置了。