Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception starting Spring application from Java

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>
like image 550
rmv Avatar asked Sep 06 '10 09:09

rmv


People also ask

What is the starting point of spring application?

The entry point of the Spring Boot Application is the class contains @SpringBootApplication annotation. This class should have the main method to run the Spring Boot application. @SpringBootApplication annotation includes Auto- Configuration, Component Scan, and Spring Boot Configuration.

Does Spring Boot work with Java 17?

Spring Boot 3.0 will require Java 17, but you don't need to wait until that release to upgrade to the latest LTS Java version. Any recent Spring Boot 2. x release will work really well with Java 17. You can also make use of Java 17 features (such as records) in your own codebases.


4 Answers

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.

like image 70
axtavt Avatar answered Oct 22 '22 10:10

axtavt


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>
like image 41
Abhinav Sarkar Avatar answered Oct 22 '22 09:10

Abhinav Sarkar


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");
}
like image 43
Ian Avatar answered Oct 22 '22 08:10

Ian


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.
like image 31
lokesh Avatar answered Oct 22 '22 08:10

lokesh