1. 問題
本文將討論Spring中最常見的配置問題 —— Spring的一個命名空間的名稱空間處理程序沒有找到。 大多數情況下,是由於一個特定的Spring的jar沒有配置在classpath下,讓我們列出多數可能出現的缺失配置以及導致的異常。
2. http://www.springframework.org/schema/security
安全名稱空間可能是迄今為止在實踐中遇到的最廣泛的問題:
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/security" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xsi:schemaLocation=" http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.1.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd"> </beans:beans>
導致以下異常:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security] Offending resource: class path resource [securityConfig.xml]
解決方法很簡單 —— 把spring-security-config的jar配置在classpath中(如:maven的pom.xml):
<dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>3.1.4.RELEASE</version> </dependency>
配置正確的名稱空間處理程序 —— 在這種情況下classpath下的SecurityNamespaceHandler會解析安全名稱空間中的元素。
3. http://www.springframework.org/schema/aop
發生在使用aop名稱空間時,沒有將相應的spring的jar配置在classpath下:
<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-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> </beans>
導致以下異常:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/aop] Offending resource: ServletContext resource [/WEB-INF/webConfig.xml]
解決方法與問題2類似,只需將spring-aop的jar配置calsspath下:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>3.2.5.RELEASE</version> </dependency>
4. http://www.springframework.org/schema/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-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> </beans>
導致以下異常:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/tx] Offending resource: class path resource [daoConfig.xml]
解決方法,將事務的jar配置到classpath下:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>3.2.5.RELEASE</version> </dependency>
5. http://www.springframework.org/schema/mvc
下面是spring的mvc名稱空間
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> </beans>
導致以下異常:
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/mvc] Offending resource: class path resource [webConfig.xml]
遇到這種異常,是因為沒有將spring的mvc的jar配置在classpath中:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.5.RELEASE</version> </dependency>
6.結論
最后,如果你是使用Eclipse來管理web服務器和部署,確保部署的組裝部分項目是配置正確的 —— 即Maven的依賴,實際上是在部署時包含在類classpath中。