从Java启动Spring应用程序的exception

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

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

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

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

使用文件/META-INF/spring.schemas/META-INF/spring.handlers解析Spring命名空间处理程序。 因为具有这些名称的文件存在于不同的Spring jar中,所以在maven-assembly-plugin之后可能只有其中一个保留在目标jar中。

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

我怀疑你的spring配置文件缺少context XML命名空间。 它应该添加到spring配置文件的根元素中,如下所示:

      

根据axtavt的回复,我发现了根本问题,我把它报告为一个?bug? 在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"); } 

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

  1. tx jar文件应包含在项目的classpath / buildpath中。 (最常见的错误)
  2. 上面提到的“axtavt”
  3. 在“axtaxt”解决方案之前尝试:转到war目录(在GWT compile的高级选项卡中指定)并将spring-tx.jar文件放在war目录下的lib文件夹中,刷新并再次运行。

你的pom有什么spring依赖? 由于某些spring jar文件不在类路径中,因此可能会出现xml解析错误。 在春季3,该库被分成许多jar文件。 看看这篇文章 ,看看你需要什么,具体来说:

  org.springframework spring-context ${org.springframework.version}  

因为它表明在Request或Response中解析存在问题。 如果RestTemplate客户端期望来自资源的特定响应,但资源完全返回一些内容,则会发生此错误。 我在POST RestTemplate客户端遇到此错误,该客户端与返回的响应中的更改有关。

例如,返回实体’MyClass’的Initial RestTemplate已更改并注视返回String,因此Parser开始出错

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

变成

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

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