Spring应用上下文模式中的错误

22

我在Eclipse中有一个maven-spring工程,其中一个spring上下文出现了这个烦人的错误消息:

引用的文件包含错误(jar:file:/M2_HOME/repository/org/springframework/spring-beans/3.1.2.RELEASE/spring-beans-3.1.2.RELEASE.jar!/org/springframework/beans/ factory/xml/spring-tool-3.1.xsd)。欲了解详细信息,请右键单击问题视图中的消息并选择“显示详细信息…”

在“显示详细信息”中看到以下内容:

enter image description here

我正在使用spring-data-jpa 1.2.0.RELEASE,我的其他spring jar版本为3.1.3.RELEASE。关于spring-data-commons-core-我甚至在我的POM中都没有依赖它,但我可以在我的m2存储库中找到它以及spring-data-commons-parent,两者的版本均为1.4.0.RELEASE。我不知道为什么会这样(也许它们是spring-data-jpa的一部分?)

我的应用程序上下文模式如下:

<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    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
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
   http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd">

我不明白为什么我一直会遇到这个错误。基本上它没有任何效果,应用程序编译、部署和运行都很好,只是这个让我疯狂的恼人的红色错误标记在Eclipse中。

16个回答

23

我最近在最新版本的Eclipse(Kepler)遇到了类似的问题,并通过禁用“首先处理所有XML模式位置”选项来解决它。具体步骤是:进入Preferences > XML > XML Files > Validation,取消勾选该选项。

这将禁用对引用指向不同模式位置的相同命名空间的验证,仅考虑在正在验证的XML文件中找到的第一个位置。该选项来自于Xerces库。

WTP文档:http://www.eclipse.org/webtools/releases/3.1.0/newandnoteworthy/sourceediting.php

Xerces文档:http://xerces.apache.org/xerces2-j/features.html#honour-all-schemaLocations


适用于Eclipse Mars的Spring混乱项目。 - Sam Su

15

我通过以下三个步骤解决了这个问题:

  1. 将此仓库添加到我的POM文件中:

    <repository>
        <id>spring-milestone</id>
        <name>Spring Maven MILESTONE Repository</name>
        <url>http://repo.springsource.org/libs-milestone</url>
    </repository>
    
  2. 我正在使用这个版本的spring-jpa:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency>
    
  3. 我从我的上下文中移除了xsd版本(虽然我不确定是否必要):

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
      xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx"
      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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
       http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
    
    我希望这可以帮助到您。


1
这个东西非常好用,对于mongodb也适用(有几个技巧)。我将其作为单独的答案添加了进去,谢谢! - tugcem

11

有时候,在下一次打开eclipse时,spring配置xml文件可能无法正常工作。

它显示由模式定义引起的xml文件错误,无论是重新打开eclipse还是清理项目都没有用。

但试试这个!

右键单击spring配置xml文件,然后选择“验证”。

过一会儿,错误就消失了,eclipse告诉你这个文件没有错误。

真是一个笑话...


8

我使用spring-data-jpa-1.3时,做的是将xsd文件的版本降低到了1.2,并且添加了一个版本号。这样错误信息就消失了。像这样:

<beans
        xmlns="http://www.springframework.org/schema/beans"
        ...
        xmlns:jpa="http://www.springframework.org/schema/data/jpa"
        xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    ...
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa-1.2.xsd">

看起来它在1.2版本中被修复了,但在1.3版本中又出现了。


6

最近我遇到了Spring 4.0的同样问题。

这是由于spring-beans-4.0.xsdspring-context-4.0.xsd中名称的冲突引起的。 打开spring-context-4.0.xsd,你会看到spring-beans-4.0.xsd被如下导入:

<xsd:import namespace="http://www.springframework.org/schema/beans"  
schemaLocation="http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"/>

这些名称冲突导致Eclipse出现“......一个模式不能包含两个具有相同名称的全局组件”的问题。值得注意的是,我在Eclipse Kepler SR2中没有遇到这个问题,但是在比较了XML验证的偏好设置后,发现它们是相同的。通过从xsi:schemaLocation属性中删除spring-context-4.0.xsd来解决了这个问题。
http://www.springframework.org/schema/context   
http://www.springframework.org/schema/context/spring-context-4.0.xsd

之后一切都按预期工作。


1
但是如果您正在使用<context: component-scan base-package="org.vimal.spring.service" />,这并不会解决问题。我还没有找出问题所在。 - vimal krishna

4

最近我遇到了与JPA-1.3相关的问题

除非我使用显式的tools.xsd链接,否则什么都不起作用。

xsi:schemaLocation=" ...
    http://www.springframework.org/schema/tool
    http://www.springframework.org/schema/tool/spring-tool-3.2.xsd
    ... ">

就像这样:

<?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:tx="http://www.springframework.org/schema/tx"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/data/jpa
        http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/tool
        http://www.springframework.org/schema/tool/spring-tool-3.2.xsd
        ">

同样的问题出现在Spring context 4.2版本中,我已经解决了。有趣的是,http://www.springframework.org/schema/context/spring-context-4.2.xsd链接到了4.1版本的Spring工具。 - borjab
谢谢,只需添加这两个schemaLocations对我来说就可以了。 http://www.springframework.org/schema/tool 和 http://www.springframework.org/schema/tool/spring-tool-3.2.xsd - Supercoder

4
如果你无法控制这些文件,因为这些文件可能是其他项目的一部分,你没有权限进行任何更改,那么你可以通过在eclipse中进行以下操作来避开这些错误:首先进入Preferences --> XML --> XML Files --> Validation --> Referenced file contains errors --> 选择 Ignore 选项。然后让该项目进行验证,错误消息将消失。

3

What @forhas和@HRgiger做的也对我有用。我使用的是spring-data-mongodb而不是jpa

但是,对于mongodb绑定,您不应该删除mongodb引用xsd的版本,只需将其保留为http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd版本即可。应删除contextbeans版本。


2

我通过更新版本号来解决了这个错误。

  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 

由于我在pom中将spring版本更新为4.3.7.RELEASE,因此需要升级到4.3。


1
我也遇到了这个问题,通过从XSD名称中删除版本部分来解决它。
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd to http://www.springframework.org/schema/beans/spring-beans.xsd

版本小于XSD的将会映射到应用程序中使用的框架的当前版本。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接