gpt4 book ai didi

java - 第一次使用 XML,这个错误是什么意思?

转载 作者:行者123 更新时间:2023-12-01 07:34:24 26 4
gpt4 key购买 nike

这是我第一次在 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

最佳答案

它的意思正是它所说的:

learnxml.LearnXML 无法转换为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");

关于java - 第一次使用 XML,这个错误是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14031944/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com