75

最初の春のプロジェクトを試しているときに、次のエラーが発生します。

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'context:component-scan

は次のapplicationContext.xmlとおりです。

<?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:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       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">

        <context:component-scan base-package="com.xyz" />

</beans>

エラーの原因は何ですか?

4

15 に答える 15

157

コンテキスト名前空間のスキーマの場所を指定していません。これが、この特定のエラーの理由です。

<beans .....
  xmlns:context="http://www.springframework.org/schema/context"
  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
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">
于 2012-11-27T17:21:57.277 に答える
6

問題がありました

Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'security:http'

私にとっては、クラスパスに spring-security-config jar を追加する必要がありました

http://docs.spring.io/spring-security/site/docs/3.1.x/reference/ns-config.html

編集:

pom に正しい依存関係がある可能性があります。

しかし...

複数の春の依存関係を使用し、単一の jar にアセンブルしている場合は、別の春の依存関係META-INF/spring.schemasによって上書きされている可能性がspring.schemasあります。

(組み立てたjarからそのファイルを抽出すると、理解できます)

Spring スキーマは、次のような一連の行にすぎません。

http\://www.springframework.org/schema/p=org.springframework.beans.factory.xml.SimplePropertyNamespaceHandler
http\://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd

ただし、別の依存関係がそのファイルを上書きすると、定義は http から取得され、ファイアウォール/プロキシがある場合は取得に失敗します。

1 つの解決策は、spring.schemas と spring.handlers を 1 つのファイルに追加することです。

小切手:

単一の jar で複数の spring 依存関係をマージするときに spring.handlers/spring.schemas が上書きされるのを回避するためのアイデア

于 2013-10-16T05:46:33.417 に答える
4

このエラーは、必要な XSD を含む jar ファイルがデプロイされたクラス パスに含まれていない場合にも発生する可能性があります。

コンテナーで依存関係が利用可能であることを確認してください。

于 2015-11-04T03:07:22.930 に答える
3

STSを使用している場合、Eclipse で構成ファイルを「Bean 構成」ファイルとしてマークできます (XML ファイルの作成時または右クリック時に指定できます)。

Spring ツール > Bean 構成として追加

プロジェクトには Spring Nature が必要です (たとえば、maven プロジェクトを右クリックします)。

Spring ツール > Spring プロジェクトの性質を追加

その後spring.xml、Spring Config Editor でデフォルトで開かれます

アプリケーションで開く > Spring Config Editor

このエディターには名前空間タブがあります

Spring Config Editor - 名前空間タブ

これにより、名前空間を指定できます。

Spring Config Editor - 名前空間の例

依存関係 (maven プロジェクトを使用) に依存することに注意してください。したがってspring-tx、maven の pom.xml で定義されていない場合、オプションが存在しないため、一致するワイルドカードは厳密ですが、要素の宣言が見つかりません「tx:annotation-driven」「context:component-scan」の問題...

于 2016-01-14T12:18:05.497 に答える
2

xml に context:component-scan を初めて追加するときは、次を追加する必要があります。

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">
于 2017-12-15T08:07:10.947 に答える
1

以下に示すように、http で xmlns コンテキストを追加します。

xmlns:context="http://www.springframework.org/schema/context"

于 2019-01-26T06:56:40.547 に答える
1

この 2 つのスキーマの場所を追加します。不要なスキーマをすべて追加する代わりに、それで十分かつ効率的です

 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context.xsd
于 2019-03-26T16:55:15.317 に答える
1

正しいパスは「/」で終わってはいけません。間違っていたので問題が発生しました

正しい方法:

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

于 2018-05-14T22:46:51.417 に答える
1

ここで言及されていない可能性が 1 つありますが、重要だと思います。

通常、検証を実行するときは、xsi:schemaLocaation提供されたパラメーターに関して実行する必要があります。つまり、バリデーターがスキーマをコンテキストにロードしていない場合、提供された場所からスキーマをオフロードしようとします。ただし、デフォルトでは、Java で使用される Xerces パーサーは、スキーマのオフロードを無効にするUSE_GRAMMAR_ONLY_POOL_FEATURE ( http://apache.org/xml/features/internal/validation/schema/use-grammar-pool-only ) というフラグを有効にします。 . したがって、バリデーターの文法プールにスキーマをプリロードしていない場合は、そのメッセージが表示されます。

問題をさらに複雑にしているのは、この機能をバリデーターで直接有効にできないことです。スキーマ ファクトリのより広い範囲で有効にする必要があります。

SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
    sf.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.USE_GRAMMAR_POOL_ONLY_FEATURE, false);
    ...
}
于 2021-05-24T11:27:16.813 に答える