从Java启动Spring应用程序的异常

rmv*_*rmv 15 java spring maven-2 maven-assembly-plugin

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

mvn -e clean compile exec:java -Dexec.mainClass=de.fraunhofer.fkie.tet.vmware.manager.Test
Run Code Online (Sandbox Code Playgroud)

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

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'.
Run Code Online (Sandbox Code Playgroud)

我还尝试将模式定义(即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>
Run Code Online (Sandbox Code Playgroud)

axt*_*avt 19

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

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

  • @rmv或者你可以让maven-dependency-plugin为你做副本,如[here](http://stackoverflow.com/questions/574594/how-can-i-create-an-executable-jar-与依赖性-使用-行家/ 4323501#4323501) (3认同)
  • 是的,你是对的,就是这样!来自不同罐子的独立'/ META-INF/spring.handlers'文件在最后一个罐子里丢失了.面对这一点,我决定暂时不使用'maven-assembly-plugin',但是从'target/lib /'目录手动包含所有jar(在'mvn install'之后),例如'java -cp target/lib/spring-aop-3.0.4.RELEASE.jar:target/lib/spring-asm-3.0.4.RELEASE.jar:[...]:target/classes de.fraunhofer.fkie.tet.vmware.manager.Test ".这按预期工作.非常感谢你!;-) (2认同)

Abh*_*kar 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>
Run Code Online (Sandbox Code Playgroud)