XMLスキーマ名前空間のSpringNamespaceHandlerが見つかりません
1. 問題
この記事では、Springで最も一般的な構成の問題の1つについて説明します–Spring名前空間の1つの名前空間ハンドラーが見つかりません。 ほとんどの場合、これは1つの特定のSpring jarがクラスパスから欠落していることを意味します。したがって、これらの欠落しているスキーマと、それぞれの欠落している依存関係について見ていきましょう。
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.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.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依存関係がプロジェクトのクラスパスにありません。
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>3.2.5.RELEASE</version>
</dependency>
これにより、正しい名前空間ハンドラー(この場合は SecurityNamespaceHandler )がクラスパスに配置され、security名前空間の要素を解析できるようになります。
完全なSpringSecurityセットアップの完全なMaven構成は、以前のMavenチュートリアルにあります。
3. http://www.springframework.org/schema/aop
クラスパスに必要なaopスプリングライブラリがない状態で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-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.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]
解決策も同様です。spring-aopjarをプロジェクトのクラスパスに追加する必要があります。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
この場合、 AopNamespaceHandler は、新しい依存関係を追加した後、クラスパスに存在します。
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-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
</beans>
正しいjarがクラスパス上にない場合も、例外が発生します。
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]
ここで欠落している依存関係はspring-txです。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
これで、右側の NamspaceHandler 、つまり TxNamespaceHandler がクラスパスに表示され、XMLとアノテーションの両方を使用した宣言型トランザクション管理が可能になります。
5. http://www.springframework.org/schema/mvc
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-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.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です。
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.0.RELEASE</version>
</dependency>
これをpom.xmlに追加すると、 MvcNamespaceHandler がクラスパスに追加され、プロジェクトが名前空間を使用してMVCセマンティクスを構成できるようになります。
6. 結論
最後に、Eclipseを使用してWebサーバーを管理し、デプロイする場合は、プロジェクトのデプロイメントアセンブリセクションが正しく構成されていることを確認してください。つまり、デプロイメント時にMavenの依存関係が実際にクラスパスに含まれていることを確認してください。 。
このチュートリアルでは、「XMLスキーマ名前空間のSpring NamespaceHandlerが見つからない」問題の通常の容疑者について説明し、発生するたびに解決策を提供しました。