Labels

Monday, June 18, 2012

Spring Exception: org.xml.sax.SAXParseException Cannot find the declaration of element ‘beans’

Problem Statement
When i run my Spring Application in App Server (Tomcat in my case), i get the following error:
SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 3 in XML document from ServletContext resource [/WEB-INF/spring/root-context.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:396)
...
at java.lang.Thread.run(Thread.java:662)
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
...
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
... 23 more

Solution
This can be caused by Spring not being able to find the relevant XSD(spring-beans-3.1.xsd in example below) defined in Spring Config File.
In my case i have these lines at the beginning of Spring Config File:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

If you are behind a Proxy(to access internet), add the following lines my Tomcat startup – this resolves the problem:
-Dhttp.proxyHost=whatever -Dhttp.proxyPort=whatever -Dhttp.proxyUser=whatever

No comments:

Post a Comment