Exception starting Spring application from Java

rmv picture rmv · Sep 6, 2010 · Viewed 67.1k times · Source

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>

Answer

axtavt picture axtavt · Sep 6, 2010

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.