1. 程式人生 > >文件根元素 "beans" 必須匹配 DOCTYPE 根 "null"

文件根元素 "beans" 必須匹配 DOCTYPE 根 "null"

昨天在測試spring的配置檔案spring.xml時,Junit測試老是報錯,其中主要錯誤資訊為: 
Caused by: java.util.InvalidPropertiesFormatException: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 80;   文件根元素 "beans" 必須匹配 DOCTYPE 根 "null"。

 檢查了好多遍spring.xml頭部的配置資訊,覺得沒問題啊,為了確保正確,於是去另外一個正確配置的專案中直接拷貝了頭部配置,但是問題依舊存在!

 

 

在網上搜了好多解決方案,都說是沒有結束標籤、標籤包裹錯誤、格式錯誤什麼的,搞了一個晚上,內心是極度崩潰的!!後來無意中看到 “重複配置了同一個bean配置檔案”的字眼,此時恍然大悟,於是回去把引入的配置檔案註釋掉

 

重新進行JUnit測試,問題解決。至此,終於明白其錯誤是<beans></beans>配置重複引起的。此類問題就像是在引用外部指令碼JS檔案時,在外部指令碼檔案中,只能包含指令碼語言程式碼,不能包含<script>標籤。

針對於上面spring.xml配置出錯解決方案其實很簡單,只需要把

    <context:property-placeholder location="classpath:spring-redis.xml" />
    <context:property-placeholder location="classpath:spring-shiro.xml" />
    <context:property-placeholder location="classpath:mongo-config.xml"/>

改成<import resource="classpath:spring-redis.xml" />
       <import resource="classpath:spring-shiro.xml" />
       <import resource="classpath:mongo-config.xml" />

即可,說到底還是自己太年輕了,竟不知道<context:property-placeholder>標籤與 <import>標籤的區別