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

use*_*796 64 java 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
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

是什么导致错误?

Bij*_*men 135

您尚未指定上下文命名空间的架构位置,这是此特定错误的原因:

<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">
Run Code Online (Sandbox Code Playgroud)

  • 我的问题是我指定了*错误的*架构位置。最好仔细检查一下,或者从这里复制/粘贴。 (2认同)

小智 6

架构位置的此路径是错误的:

http://www.springframework.org/schema/beans
Run Code Online (Sandbox Code Playgroud)

正确的路径应该以/:

http://www.springframework.org/schema/beans/
Run Code Online (Sandbox Code Playgroud)

  • 尾部斜线与问题无关。这个答案不应该像目前那样有那么多的赞成票。 (4认同)
  • 我不太确定这是真的。 (3认同)

Dav*_*ala 5

我遇到了问题

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'
Run Code Online (Sandbox Code Playgroud)

对我来说,我必须将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.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
Run Code Online (Sandbox Code Playgroud)

但是如果另一个依赖项覆盖了该文件,那么将从http中检索定义,如果你有防火墙/代理,它将无法获取它.

一种解决方案是将spring.schemas和spring.handlers附加到单个文件中.

校验:

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


swe*_*tfa 5

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

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