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

我在尝试我的第一个春季项目时出现了以下错误:

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>

是什么导致了这个错误?

141167 次浏览

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

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

我和他之间的问题

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

编辑:

这可能是你有正确的依赖在您的诗歌。

但是..。

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

(从组装好的罐子中提取文件,你就会明白)

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.schema 被覆盖的想法

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

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

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

Spring Tools > Add as Bean Configuration

你的项目必须有 SpringNature (例如右键单击 maven project) :

Spring Tools > Add Spring Project Nature

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

Open With > Spring Config Editor

这个编辑器有 Namespaces 选项卡

Spring Config Editor - Namespaces tab

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

Spring Config Editor - Namespaces example

请注意,它取决于依赖关系(使用 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 中添加 context: Component-scanner 时,需要添加以下内容。

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

正确的路径不应该以“/”结束,我错了,引起了麻烦

正确的做法:

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"

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

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

 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 连接,则会失败并导致此错误。

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 (上面只有两个地方)。

将标记错误的 译注:更改为 返回文章页面 或所有以 * . xsd 扩展名结尾的 URL。

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

通常,在执行验证时,应该针对提供的 xsi:schemaLocaation参数进行验证,即如果验证器没有在上下文中加载模式,它会尝试从提供的位置卸载模式。默认情况下,Java 使用的 Xerces 解析器启用一个名为 USE _ Grammar _ ONLY _ POOL _ FEATURE 使用 _ 语法 _ ONLY _ POOL _ 功能(翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳翻译: 奇芳 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);
...
}

将 xsi: schema 分配改为 follow 解决了我的问题

    <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"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation=
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">