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.xml 中有正确的依赖关系。

但...

如果您使用多个 spring 依赖项并组装到一个 jar 中,那么META-INF/spring.schemas可能会被spring.schemas另一个 spring 依赖项覆盖。

(从您组装的 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 附加到一个文件中。

查看:

避免在单个 jar 中合并多个 spring 依赖项时覆盖 spring.handlers/spring.schemas 的想法

于 2013-10-16T05:46:33.417 回答
6

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

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

正确的路径应该以/

http://www.springframework.org/schema/beans/
于 2017-02-08T08:39:13.303 回答
4

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

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

于 2015-11-04T03:07:22.930 回答
3

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

Spring 工具 > 添加为 Bean 配置

您的项目必须具有 Spring Nature(例如,右键单击 maven 项目):

Spring 工具 > 添加 Spring 项目性质

然后spring.xml默认使用 Spring Config Editor 打开

打开方式 > Spring 配置编辑器

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

Spring Config Editor - 命名空间选项卡

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

Spring 配置编辑器 - 命名空间示例

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

于 2016-01-14T12:18:05.497 回答
3

为时已晚,但对其他人可能有用

匹配的通配符是严格的,但找不到元素 '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">
于 2017-07-04T05:19:43.700 回答
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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd

于 2018-05-14T22:46:51.417 回答
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 -->
于 2018-08-08T09:33:41.723 回答
1

添加带有 http 的 xmlns 上下文,如下所示

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

于 2019-01-26T06:56:40.547 回答
1
<?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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">

以上链接需要包含

    <context:property-placeholder location="classpath:sport.properties" />


    <bean id="myFortune" class="com.kiran.springdemo.HappyFortuneService"></bean>

    <bean id="myCoach" class="com.kiran.springdemo.setterinjection.MyCricketCoach">
        <property name="fortuner" ref="myFortune" />

        <property name="emailAddress" value="${ipl.email}" />
        <property name="team" value="${ipl.team}" />

    </bean>


</beans>
于 2019-03-13T07:08:48.893 回答
1

添加这两个架构位置。这已经足够且高效,而不是添加所有不必要的架构

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

在各种 Spring jar 中有“META-INF/spring.schemas”文件,其中包含为本地解析而截获的 URL 的映射。如果这些文件中没有列出特定的 xsd URL(例如从 http 切换到 https 之后),Spring 会尝试从 Internet 加载模式,并且如果系统没有 Internet 连接,它会失败并导致此错误。

这可能是 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"

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(仅上面两个位置)。

于 2020-04-28T21:45:39.253 回答
1

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

于 2020-05-28T08:22:05.500 回答
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 回答