通配符匹配很严格,但是找不到元素“context:component-scan”的声明。

85

我在尝试我的第一个Spring项目时遇到以下错误:

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>

是什么导致了这个错误?

17个回答

171

您未指定上下文命名空间的模式位置,这就是出现特定错误的原因:

<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">

4
我的问题在于我指定了错误的模式位置。最好再检查一遍,或者从这里复制/粘贴。 - vadipp
只有在将现有的“xmlns:context”放置在“xsi:schemaLocation”规范之前时,错误才会消失。感谢您的建议。 - tm1701
+1. 这个答案可以通过突出显示答案与原始代码的不同之处或仅保留相关行来改进示例。 - Madbreaks
1
我正在学习“O'Reilly - Spring Framework Essentials”课程时遇到了一个错误。结果发现课程中有一个错别字: 正确的应该是“http://www.springframework.org/schema/context/spring-context.xsd”,但他们写成了错误的“http://www.springframework.org/schema/beans/spring-context.xsd”。并不怪责他们,因为没有什么是完美的,但在这里发布一下,或许能帮助到其他人。 - AbstractVoid

7

我遇到了问题

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文件中有正确的依赖。

但是...

如果您正在使用多个Spring依赖项并将其组装成单个jar包,那么META-INF/spring.schemas可能会被另一个Spring依赖项的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中检索,并且如果您有防火墙/代理,它将无法获取它。
一种解决方案是将spring.schemas和spring.handlers附加到单个文件中。
检查: 合并多个Spring依赖项到一个jar时避免覆盖spring.handlers / spring.schemas的想法

这有助于我识别我所面临的问题。我错过了在pom中添加“spring-web”依赖。 - ring bearer
同时确保在xsi:schemaLocation头中指定Spring模式的特定版本与spring.schemas文件中列出的该模式的版本以及打包在该Spring jar中的版本相匹配。 - Matthew Wise
我曾经遇到过与HTTP安全相关的完全相同的问题。根本原因是缺少spring-security-config jar包。将此jar包作为编译时Maven依赖项添加解决了我的问题。 - nirmalsingh

6

这个模式位置的路径是错误的:

http://www.springframework.org/schema/beans

正确的路径应该以 / 结尾:
http://www.springframework.org/schema/beans/

3
我不太确定这是真的。 - rslj
6
结尾斜杠与问题无关,这个回答不应该有目前这么多赞。 - Scarfe
这在我的 Spring 应用中真的很好用。非常感谢! - Purushottam

5

如果包含所需XSD的JAR文件未包含在您的部署类路径中,也会导致此错误。

确保依赖项在您的容器中可用。


1
我遇到了与安全相关的问题,如David的回答中所述。根本原因是缺少spring-security-config jar包。将此jar包作为编译时maven依赖项添加解决了我的问题。 - nirmalsingh

4

虽然已经有些晚了,但这可能对其他人有用。

匹配通配符非常严格,但是没有找到元素“context:component-scan”的声明。

这意味着您在XML中缺少一些声明未找到所需的声明。

在我的情况下,我忘记添加以下内容:

添加后,问题解决了。

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        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-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

3
如果使用 STS,您可以在 Eclipse 中将配置文件标记为“Bean 配置”文件(您可以在创建时或右键单击 XML 文件时指定)。

Spring Tools > Add as Bean Configuration

您的项目必须拥有Spring Nature(例如,在Maven项目上右键单击):

Spring Tools > Add Spring Project Nature

然后默认情况下,使用Spring Config Editor打开spring.xml

Open With > Spring Config Editor

而且这个编辑器有命名空间选项卡

Spring Config Editor - Namespaces tab

这使您能够指定命名空间:

Spring Config Editor - Namespaces example

请注意,这取决于依赖项(使用maven项目),因此如果在maven的pom.xml中未定义spring-tx,则该选项不可用,这会防止您出现匹配通配符是严格的,但找不到元素'tx:annotation-driven''context:component-scan'问题...

3

将标记错误或所有以*.xsd扩展名结尾的URL从http更改为https


这是唯一对我有用的建议。谢谢,伙计! - Anshul

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">

2
在各种Spring jars中都有'META-INF/spring.schemas'文件,其中包含被拦截进行本地解析的URL映射。如果在这些文件中没有列出特定xsd URL(例如从http切换到https后),Spring会尝试从互联网加载模式,如果系统没有互联网连接,则会失败并导致此错误。
这可能是Spring Security v5.2及更高版本的情况,其中没有xsd文件的http映射。
要修复它,请更改
xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/security  
    http://www.springframework.org/schema/security/spring-security.xsd"

to

xsi:schemaLocation="
    http://www.springframework.org/schema/beans     
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/security  
    https://www.springframework.org/schema/security/spring-security.xsd"

请注意,只有实际的xsd URL从http更改为https(仅限上述两个位置)。

1

通过命名空间声明和模式位置,您还可以检查命名空间使用的语法,例如:

<beans xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation= http://www.springframework.org/`enter code here`schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">

<context:annotation-driven/>   <!-- This is wrong -->
<context:annotation-config/> <!-- This should work -->

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接