murali2489 0 Junior Poster

Hi Team,

This is my first Spring Code . I just followed the code provided in the book and tried to run the class , but it throws the below error. Please throw light on it.

package spring.hello;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;



public class Main{


public static void main(String[] args) throws Exception{

BeanFactory factory = new XmlBeanFactory(new FileSystemResource("C:\\spring\\hello\\Hello.xml"));

HelloInterface obj = (HelloInterface) factory.getBean("obj");

obj.sayHello();




}



}



Feb 13, 2014 9:59:30 AM org.springframework.core.CollectionFactory <clinit>
INFO: JDK 1.4+ collections available
Feb 13, 2014 9:59:30 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from file [C:\spring\hello\Hello.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Line 5 in XML document from file [C:\spring\hello\Hello.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 40; cvc-elt.1: Cannot find the declaration of element 'beans'.
Caused by: org.xml.sax.SAXParseException; lineNumber: 5; columnNumber: 40; cvc-elt.1: Cannot find the declaration of element 'beans'.
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
    at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)






package spring.hello;

public class HelloImpl implements HelloInterface{

private String greeting;

public HelloImpl(){

}


public HelloImpl(String greeting) {


this.greeting = greeting;

}


public void sayHello(){

System.out.println(greeting);

}


public void setGreeting(String greeting) {


this.greeting = greeting;

}



}





package spring.hello;

public interface HelloInterface {


public void sayHello();




}

** Hello.xml file is below **

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.test.com"
xmlns:xsi="http://www.test.com"
xsi:schemaLocation="http://test.com">

<bean id="obj"
class="spring.hello.HelloImpl">
<property name="greeting" value="Hello World !" />
</bean>
</beans>

** fyi i have not created any test.com website, just created the above class file and xml file **

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.