這是我第一次在java中使用XML,我希望得到我的代碼的一些幫助。我看不到我錯在哪裏。謝謝。首次使用XML用戶,這個錯誤是什麼意思?
我想要做的是理解Spring框架。
下面是代碼:
public class Student
{
public Student(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
// VARIABLES DECLARATION
private String name = null;
}
public class Controller
{
public Controller(Student student)
{
this.student = student;
}
public void PrintName()
{
System.out.println(""+student.getName());
}
public Student getStudent()
{
return student;
}
public void setStudent(Student student)
{
this.student = student;
}
// VARIABLES DECLARATION
private Student student;
}
public class LearnXML
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
// TODO code application logic here
ApplicationContext factory = new ClassPathXmlApplicationContext("Student.xml");
Controller contr = (Controller) factory.getBean("application");
contr.PrintName();
}
public void setController(Controller controller)
{
this.controller = controller;
}
private Controller controller;
}
我的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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="domain.Student">
<constructor-arg value="Ovidiu" />
</bean>
<bean id="brain" class="controller.Controller">
<constructor-arg ref="student"/>
</bean>
<bean id="application" class="learnxml.LearnXML">
<property name="controller" ref="brain"/>
</bean>
</beans>
我的錯誤:
Dec 25, 2012 5:34:02 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org[email protected]dd0099: startup date [Tue Dec 25 17:34:02 EET 2012]; root of context hierarchy
Dec 25, 2012 5:34:03 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [Student.xml]
Dec 25, 2012 5:34:03 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
INFO: Pre-instantiating singletons in org.s[email protected]5ceeb4a3: defining beans [student,brain,application]; root of factory hierarchy
Exception in thread "main" java.lang.ClassCastException: learnxml.LearnXML cannot be cast to controller.Controller
at learnxml.LearnXML.main(LearnXML.java:25)
Java Result: 1