15

我能够使用 Maven 编译并启动我的 Spring 项目:

mvn -e clean compile exec:java -Dexec.mainClass=de.fraunhofer.fkie.tet.vmware.manager.Test

maven-assembly-plugin但是,当我使用(包括)将所有 jar 组装在一个文件中时applicationContext.xml,我总是在执行Exception期间得到一个java

java -cp target/test-jar-with-dependencies.jar:. de.fraunhofer.fkie.tet.vmware.manager.Test

  INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
  Sep 6, 2010 10:37:21 AM org.springframework.util.xml.SimpleSaxErrorHandler warning
  WARNING: Ignored XML validation warning
  org.xml.sax.SAXParseException: 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>.
  ...
  Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: 
  Line 10 in XML document from class path resource [applicationContext.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-complex-type.2.4.c: 
  The matching wildcard is strict, but no declaration can be found for element 'context:annotation-config'.
  ...
  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:annotation-config'.

我还尝试将模式定义(即spring-context.xsd等)直接附加到类路径,但没有任何成功。

less src/main/resources/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: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">

      <context:annotation-config />   <!-- wegen '@PostConstruct' -->
  <!--<context:component-scan base-package="de.fraunhofer" />     -->
  ...
  </beans>
4

6 回答 6

19

Spring 命名空间处理程序使用文件/META-INF/spring.schemas/META-INF/spring.handlers. 因为具有这些名称的文件存在于不同的 Spring jar 中,所以在maven-assembly-plugin.

也许您可以手动合并这些文件,并以某种方式配置maven-assembly-plugin为使用此合并文件覆盖目标 jar 中的文件。

于 2010-09-06T10:45:57.733 回答
5

我怀疑您的 spring 配置文件缺少contextXML 命名空间。它应该像这样添加到你的 spring 配置文件的根元素中:

<?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:annotation-config />
    <context:component-scan base-package="com.abc.xyz" />

</beans>
于 2010-09-06T09:17:50.453 回答
3

根据 axtavt 的回复,我发现了这个问题的根本问题,并且我已将其报告为?错误?在 Spring 中:https ://jira.springsource.org/browse/SPR-8368 - 包含生成您自己的这些文件的合并副本的解决方法。对于后代,代码也在这里:

//IOUtils and FileUtils come from Apache Commons IO
for(String s : new String[] {"spring.schemas", "spring.handlers", "spring.tooling"}) {
    Enumeration<?> e = Test.class.getClassLoader().getResources("META-INF/"+s);
    StringBuilder out = new StringBuilder();
    while(e.hasMoreElements()) {
        URL u = (URL) e.nextElement();
        out.append(IOUtils.toString(u.openStream())).append("\n");
    }
    File outf = new File(s);
    FileUtils.writeStringToFile(outf, out.toString(), "UTF-8");
}
于 2011-05-24T08:31:46.707 回答
1

我相信这个问题有3个解决方案

  1. tx jar 文件应包含在项目的类路径/构建路径中。(最常见的错误)
  2. 上面提到的“axtavt”
  3. 在“axtaxt”解决方案之前试试这个:进入你的war目录(在GWT编译的高级选项卡中指定)并将spring-tx.jar文件放在你的war目录下的lib文件夹中,刷新并再次运行。
于 2012-04-16T09:47:58.590 回答
0

正如它表明在请求或响应中的解析存在问题。如果 RestTemplate 客户端期望来自资源的特定类型的响应,但资源完全返回某些内容,则可能会发生此错误。对于与返回的响应中的更改相关的 POST RestTemplate 客户端,我收到此错误。

例如,返回实体“MyClass”的初始 RestTemplate 更改并盯着返回字符串,因此解析器开始给出错误

 ResponseEntity<MyClass> postResponseEntity = restTemplate.postForEntity(postEndPoint, httpRequestEntity, MyClass.class);
 MyClass postResponseBody = postResponseEntity.getBody();

变成

 ResponseEntity<String> postResponseEntity = restTemplate.postForEntity(postEndPoint, httpRequestEntity, String.class);
 String postResponseBody = postResponseEntity.getBody();

随着响应类型从实体更改为字符串,解析器在处理响应时开始出错。将其更改为正确的响应类型(在我的情况下是字符串)并开始工作。

于 2013-01-10T17:10:42.063 回答
0

你的 pom 中有哪些 spring 依赖项?由于某些 spring jar 文件不在类路径上,您可能会遇到 xml 解析错误。在 spring 3 中,该库被拆分为许多 jar 文件。查看这篇文章,看看你需要什么,特别是:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
于 2010-09-06T09:21:16.813 回答