spring載入beans找不到宣告
Spring配置檔案xsi:schemaLocation無法解析導致啟動失敗的解決方案
今天向伺服器部署一個console java app之後,啟動時發現程式出現異常:
Exception in thread “main” org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 10 in XML document from class path resource [app-config.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element ‘beans’.
然後去看看app-config.xml裡的schema宣告部分,是這樣寫的:
<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”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd” >
很明顯,有關’beans’的宣告都放在http://www.springframework.org/schema/beans/spring-beans-3.0.xsd裡面了。可能是網路原因,訪問不倒導致解析app-config.xml失敗,致使程式丟擲異常。
開啟spring-beans-3.0.1.RELEASE.jar一看,那個xsd檔案就在org.springframework.beans.factory.xml包下面,這下子好辦了,把xsd檔案的引用全改為本地的就行了:
<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”
xsi:schemaLocation=”http://www.springframework.org/schema/beans
classpath:/org/springframework/beans/factory/xml/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
classpath:/org/springframework/context/config/spring-context-3.0.xsd” >