spring框架有兩種模板,推薦使用帶命名空間的,這樣可以再編譯的時候自動檢查,還有提示功能:
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>
另一種也可以正常運行,只是不會自動檢查錯誤,如果編譯期有錯會在運行期拋出錯誤,如果applicationContext.xml文件大的話這樣會使排錯困難:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
</beans>
PS:沒有命名空間的的xml爲普通的xml文件,所以不會檢查和提示,提示效果如下:
如果能保證xml書寫結果正確的話,也可以不用寫命名空間,官方並不要求,但推薦。