Java 匹配的通配符是严格的,但找不到元素';上下文:组件扫描

Java 匹配的通配符是严格的,但找不到元素';上下文:组件扫描,java,spring,Java,Spring,我在尝试我的第一个spring项目时遇到以下错误: 原因:org.xml.sax.SAXParseException:cvc复杂类型。2.4.c:匹配的通配符是严格的,但找不到元素上下文:组件扫描的声明 以下是applicationContext.xml: 导致错误的原因是什么?您没有指定上下文命名空间的架构位置,这就是导致此特定错误的原因: 我与 Caused by: org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: The m

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

原因:org.xml.sax.SAXParseException:cvc复杂类型。2.4.c:匹配的通配符是严格的,但找不到元素上下文:组件扫描的声明
以下是
applicationContext.xml



导致错误的原因是什么?

您没有指定上下文命名空间的架构位置,这就是导致此特定错误的原因:


我与

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安全配置jar添加到类路径中

编辑:

这可能是因为您在pom中具有正确的依赖关系

但是

如果您使用多个spring依赖项并将其组装到一个jar中,那么另一个spring依赖项的
spring.schemas
可能会覆盖META-INF/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附加到单个文件中

检查:


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

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

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

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

然后默认情况下使用spring配置编辑器打开
spring.xml

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

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


请注意,它取决于依赖项(使用maven project),因此如果maven的pom.xml中未定义
spring tx
,则选项不存在,这会防止出现问题…

此模式位置路径错误:

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

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

虽然为时已晚,但可能对其他人有所帮助

匹配的通配符是严格的,但找不到的声明 元素的上下文:组件扫描

这意味着您错过了一些声明,或者在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">

在xml中首次添加上下文:组件扫描时,需要添加以下内容

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

正确的路径不应该以“/”结尾,我的错误导致了麻烦

正确的方式:


通过名称空间声明和架构位置,您还可以检查名称空间使用的语法,例如:-

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

使用http添加xmlns上下文,如下所示

xmlns:context=”http://www.springframework.org/schema/context“


以上链接需要包括在内


添加这两个架构位置。这就足够了,而且效率很高,而不是添加所有不必要的模式

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

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

在SpringSecurityV5.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"


请注意,只有实际的xsd URL被从http修改为https(上面只有两处)。

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

这里有一种可能性没有提到,但我认为很重要

通常,当您执行验证时,应该根据提供的
xsi:schemaLocation
参数进行验证,也就是说,如果验证器没有在上下文中加载架构,它会尝试从提供的位置卸载它但是,默认情况下,Java使用的Xerces解析器启用一个名为仅使用语法\u池\u功能的标志(),该标志禁用任何模式卸载。因此,如果您没有在验证器的语法池中预加载模式,您将遇到该消息

使问题进一步复杂化的是,无法在验证器中直接启用该功能。您必须在架构工厂的更大范围内启用它:

SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
    sf.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.USE_GRAMMAR_POOL_ONLY_FEATURE, false);
    ...
}

这有助于我确定我所面临的问题。我错过了在POM中添加
spring web
依赖项,也没有确保如果在xsi:schemaLocation头中指定spring模式的特定版本,该版本与spring.schemas文件中列出并捆绑在该spring jar中的该模式的版本相匹配。我遇到了与安全http完全相同的问题。根本原因是缺少spring安全配置jar。添加这个jar作为编译时maven依赖项为我解决了这个问题。我的问题是我指定了错误的模式位置。最好仔细检查,或者从这里复制/粘贴。只有将现有的“xmlns:context”放在建议的“xsi:schemaLocation”规范之前时,错误才会消失。+1。这个答案可以通过Highlight来改进
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"
SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
    sf.setFeature(Constants.XERCES_FEATURE_PREFIX + Constants.USE_GRAMMAR_POOL_ONLY_FEATURE, false);
    ...
}