从 Java 启动 Spring 应用程序时出现异常

发布于 2024-09-17 16:48:19 字数 2422 浏览 15 评论 0原文

我可以使用 Maven 编译并启动我的 Spring 项目:

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

但是,当我使用 maven-assemble-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 <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>

I am able to compile and start my Spring project using Maven:

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

However, when I assemble all jars in a single file using the maven-assembly-plugin (including applicationContext.xml), I always get an Exception during the java execution:

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

I also tried to attach the schema definitions, i.e. spring-context.xsd etc., directly to the classpath, but without any success.

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

半边脸i 2024-09-24 16:48:19

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

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

Spring namespace handlers are resolved using files /META-INF/spring.schemas and /META-INF/spring.handlers. Because files with these names exist in different Spring jars, probably only one of them remains in target jar after maven-assembly-plugin.

Perhaps you may merge these files manually and somehow configure maven-assembly-plugin to overwrite file in target jar with this merged file.

鸩远一方 2024-09-24 16:48:19

我怀疑您的 spring 配置文件缺少 context XML 命名空间。它应该添加到 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>

I suspect your spring config file is missing the context XML namespace. It should be added to the root element of your spring config file like this:

<?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>
一个人的旅程 2024-09-24 16:48:19

我根据 axtavt 的回复找到了根本问题,并将其报告为“bug”在春季: 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");
}

I've found the root problem for this as per the reply of axtavt, and I've reported it as a ?bug? in Spring: https://jira.springsource.org/browse/SPR-8368 - a workaround to generate your own merged copies of these files is included there. For posterity the code is here as well:

//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");
}
回眸一笑 2024-09-24 16:48:19

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

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

I believe there are 3 solutions to this problem

  1. The tx jar file should be included in the classpath/buildpath of the project. (most common error)
  2. Mentioned by "axtavt" above
  3. Try this before "axtaxt" solution: go to your war directory (specified in GWT compile's advanced tab) and put the spring-tx.jar file in the lib folder under your war directory, refresh and run again.
千纸鹤带着心事 2024-09-24 16:48:19

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

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${org.springframework.version}</version>
</dependency>

What spring dependencies do you have in your pom? You may get xml parsing errors due to some spring jar files not being on the class path. In spring 3 the library was split up into many jar files. Check out this post to see what you need, specifically:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${org.springframework.version}</version>
</dependency>
鸠书 2024-09-24 16:48:19

正如它表明请求或响应中的解析存在问题。如果 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();

随着响应类型从实体更改为字符串,解析器在处理响应时开始给出错误。将其更改为正确的响应类型(在我的例子中是字符串),它开始工作。

As it suggests there is a problem with the parsing either in Request or in Response. This error can occur if the RestTemplate client is expecting particular kind of Response from the Resource, but the Resource is returning something entirely. I got this error for a POST RestTemplate client that was related to change in the Response returned.

For e.g. Initial RestTemplate that was returning an Entity 'MyClass' changed and stared returning String so the Parser started giving error

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

Changed to

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

As the response type changed from the entity to String the parser started giving error when processing the Response. Changed it to correct Response type (which in my case was String) and it started working.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文