10

I'm trying to set up a simple spring application and I'm getting the below exception. This is being run standalone in eclipse indigo.

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Line 2 in XML document from class path resource [context.xml] is invalid; nested exception is org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'beans'.
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 com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:131)
      at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:384)
      at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:318)

Here's the initial portion of my code:

public static void main(String[] args) {

        try {

            BeanFactory beanfactory = new ClassPathXmlApplicationContext(
                    "context.xml");


            FirstBean bean = (FirstBean) beanfactory.getBean("show");

Here's my context.xml file:

<?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:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="anotherBean" class="AnotherBean" />
<bean id="show" class="FirstBean">
<constructor-arg ref="anotherBean" />
</bean>
<bean id="populateFD" class="PopulateFactData">
<constructor-arg value="localhost" />
<constructor-arg value="3309" />
</bean>
</beans>
opike
  • 7,053
  • 14
  • 68
  • 95

5 Answers5

13

Are you sure you have spring-beans on the classpath?

This error normally means that it can't find a spring.schemas (which is in spring-beans.jar) explaining to it what that namespace means.

Other options are that the Maven Shade plugin has damaged spring.schemas, but that's unlikely to be the case as you haven't mentioned Maven.

FauxFaux
  • 2,445
  • 16
  • 20
  • It was a jar/classpath issue. I was using a small set of spring jars that came with a tutorial. After I downloaded the full package from springsource and added those jars into my buildpath, the error went away. – opike Apr 17 '12 at 16:06
1

Maybe this post can help you:

Cannot find the declaration of element 'beans' in internet offline mode

It seems like being a problem of Schema configuration.

Community
  • 1
  • 1
Mik378
  • 21,881
  • 15
  • 82
  • 180
1

the maven shade plugin seems to replace the spring.schemas file in the jars,so creating one of our own with all the individual spring.schema contents from each of the jar should solve the issue.

sathishvisa
  • 315
  • 1
  • 5
  • 16
1

When I had this issue in STS I just cleaned the project and it worked.

emurano
  • 973
  • 6
  • 15