【问题标题】:The matching wildcard is strict, but no declaration can be found for element 'tx:annotation-driven' 'context:component-scan'匹配的通配符是严格的,但找不到元素 'tx:annotation-driven' 'context:component-scan' 的声明
【发布时间】:2016-04-19 04:28:38
【问题描述】:

谁能找出下面的命名空间声明有什么问题?我已经提到了所有的命名空间并给出了对模式文件的引用。不知道还缺少什么。

<?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: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.xsd    
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd    
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd    
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com.mypackage" />
    <context:annotation-config />
    <tx:annotation-driven />

</beans>

错误信息:

在这一行找到多个注释: - cvc-complex-type.2.4.c:匹配通配符是严格的,但找不到元素'tx:annotation-driven'的声明。 - schema_reference.4:无法读取架构文档“http://www.springframework.org/schema/tx/spring-tx.xsd”,因为 1) 找不到文件; 2) 文件无法读取; 3) 文档的根元素不是&lt;xsd:schema&gt;

【问题讨论】:

标签: java xml spring xsd


【解决方案1】:

我更喜欢 STS 而不是 Eclipse,这就是重现问题的方式......

我有一个带有 Spring 上下文依赖项的 Maven 项目:

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>betlista</groupId>
    <artifactId>tests.so.q34779551</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SO-q34779551</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.2.4.RELEASE</version>
        </dependency>
    </dependencies>

</project>

当我现在在 src/main/resources 中创建与您相同的 spring.xml 时,我遇到了同样的问题:

schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/beans/spring-beans.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/context/spring-context.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/context/spring-context.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
schema_reference.4: Failed to read schema document 'http://www.springframework.org/schema/tx/spring-tx.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.

问题是,spring-txspring-context 不在依赖项中 - 添加后,错误已修复。

更多详情请见this answer

【讨论】:

  • 但我的项目依赖项中确实有 tx 和上下文 jar。部署到 RAD 上的 websphere 时,应用程序运行良好。这只是 RAD (Eclipse) 抱怨..
  • 在 Eclipse 中可能有很多东西,例如代理...您也可以查看 this answer 以获取本地架构版本...
【解决方案2】:

我在使用 Maven 创建 Spring MVC 项目时遇到了同样的错误,它会自动添加核心依赖项,但有时构建不会成功,在这种情况下,如果您启用带注释的组件扫描,则会出现上述错误。

解决方案:只需通过添加更新 Pom.xml 中的 Spring Core Dependencies 根据您的 Spring 版本在下面标记。

<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.2.4.RELEASE</version>

【讨论】:

    猜你喜欢
    • 2011-08-28
    • 2012-11-15
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 2014-09-06
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多