分享知識 傳遞快樂
Spring是如何校驗XML的:
Spring默認在啓動時是要加載XSD文件來驗證xml文件的,所以如果有的時候斷網了,或者一些開源軟件切換域名,那麼就很容易碰到應用啓動不了。爲了防止這種情況,Spring提供了一種機制,默認從本地加載XSD文件。
例如:可以打開spring-context-4.3.0.RELEASE.jar,進入org/springframework/context/config/目錄,可以看到下面有
spring-context-xxx.xsd
spring-context-xxx.xsd
spring-context-xxx.xsd
spring-context-xxx.xsd
......
Spring中的xsd文件帶版本號與不帶版本號的區別:
不帶版本號,它會自動使用JAR中最新的xsd;這樣以後升級Spring版本的時候,你的配置文件不用再手動改動。Spring的配置裏,最好不要配置xsd文件的版本號。
另外如果沒有配置版本號,取的就是當前jar裏的XSD文件,減少了各種風險。其實,最大的好處是,當你升級了Spring的jar,該配置文件的XSD聲明部分也不用改,會自動找到最新的本地jar裏的XSD聲明。Spring做了特殊處理,保證斷網的情況下不寫XSD版本號,能找到本地的jar文件。
完整的
<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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
</beans>
MVC
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>
AOP
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
TX
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
</beans>
TASK
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd">
</beans>
如果用到的其它配置信息可以引用
<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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
例:
如果在啓動項目時報:
org.xml.sax.SAXParseException; lineNumber: 11; columnNumber: 76; 元素 "context:property-placeholder" 的前綴 "context" 未綁定。
追溯到配置文件中,如果是
<context:property-placeholder location="classpath:config/db.properties" />
引起的,那麼要bean頭部加入以下信息即可:
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
......
">