这是我第一次在java中使用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.springframework.context.support.ClassPathXmlApplicationContext@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.springframework.beans.factory.support.DefaultListableBeanFactory@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
答案 0 :(得分:4)
这正是它所说的:
learnxml.LearnXML cannot be cast to controller.Controller
您有LearnXML
个对象:
<bean id="application" class="learnxml.LearnXML">
<property name="controller" ref="brain"/>
</bean>
正在尝试将其转换为Controller
对象:
Controller contr = (Controller) factory.getBean("application");
由于手头任务的固有性质, getBean()
正在返回Object
而不是特定类型;它使用反射来实例化xml中的任何类型,因此它不知道编译时类型是什么。在运行时,如果您尝试将Object
转换为某些内容而不是......那么您将获得该异常。
编辑添加:
如果不明显,看起来你真的想要这样做:
LearnXML lXML = (LearnXML) factory.getBean("application");
答案 1 :(得分:1)
你来到这里factory.getBean("application");
LearnXML类型对象,但您将其转换为Controller
如果你想让Controller像这样使用大脑
Controller contr = (Controller) factory.getBean("brain");
答案 2 :(得分:1)
Controller contr =(Controller)factory.getBean(“application”); 您无法将LearnXML对象强制转换为Controller对象
试试这个。 LearnXML learnXml =(LearnXML)factory.getBean(“application”); 。learnXml.getController()PrintName();
答案 3 :(得分:1)
在代码
下更改代码LearnXML learnXML =(LearnXML)factory.getBean(“application”);