標籤:applicationcontext.x applicationcontext.x applicationcontext.x
最近在項目上,遇到了一個問題,在配置applicationContext.xml使用<import>標籤引入其它的xml檔案時,導致項目啟動時過慢,有時還會引起啟動異常,後來查到是xml檔案頭中的設定問題,雖然不太知道標籤頭的作用,但還是和大家分享一下經驗:
applicationContext.xml檔案,只貼出了部分代碼:
<!-- 檔案頭-->
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.0.xsd">
<!-- 注入baseDAO層,用於其它的spring.xml檔案繼承 -->
<bean id="baseDao" class="framework.base.BaseDao">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<import resource="com/shipment/dao/shipmentSpring.xml" />
引入的shipmentSpring.xml檔案:
大家可以看到,第二個檔案shipmentSpring.xml中的檔案頭,xsi:schemaLocation多了兩行出來
下面是啟動時控制台的資訊,貼出部分異常的資訊:
org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 78; schema_reference.4: 無法讀取方案文檔 ‘http://www.springframework.org/schema/beans/spring-beans-3.0.xsd‘, 原因為 1) 無法找到文檔; 2) 無法讀取文檔; 3) 文檔的根項目不是 <xsd:schema>。
Caused by: java.net.SocketException: Software caused connection abort: recv failed
五月 12, 2014 9:55:49 上午 org.apache.catalina.core.StandardContext listenerStart
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [com/shipment/dao/shipmentSpring.xml]
Offending resource: class path resource [applicationContext.xml]; nested exception is org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:Line 9 in XML document from class path resource [com/shipment/dao/shipmentSpring.xml] is invalid; nested exception is org.xml.sax.SAXParseException;lineNumber: 9; columnNumber: 78; cvc-elt.1: 找不到元素 ‘beans‘ 的聲明。
Caused by: org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException:Line 9 in XML document from class path resource [com/shipment/dao/shipmentSpring.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 78; cvc-elt.1: 找不到元素 ‘beans‘ 的聲明。
Caused by: org.xml.sax.SAXParseException; lineNumber: 9; columnNumber: 78; cvc-elt.1: 找不到元素 ‘beans‘ 的聲明。
啟動時,控制後經常停留在以下兩行位置:
大家可以看到我用紅色標註的字型,全都是第9行,再看看shipmentSpring.xml檔案的第9 行。一開始我注意到這裡,後來我將applicationContext.xml的檔案頭複製到shipmentSpring.xml裡,將shipmentSpring.xml原來的檔案頭刪除了,項目就能正常啟動了,且啟動速度快很多
五月 12, 2014 10:08:22 上午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 7933 ms
原來的啟動速度(這是原來的配置正常啟動的速度,異常時啟動完成的時間30多秒):
五月 12, 2014 9:55:50 上午 org.apache.catalina.startup.Catalina start
INFO: Server startup in 21174 ms
說明一下:這兩個檔案的檔案頭我都是在別人的項目那裡複製過來的,別人那裡能正常啟動,且啟動完成時間也是10秒內。
檔案頭的作用(不知道對不對,大家自己去查查):我印象中好像是在指定的路徑裡找到相應的檔案,也起到限制標籤書寫是否正確的作用