2012-12-25 55 views
0

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

回答

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

如果您聽到這裏factory.getBean("application");

LearnXML型objec噸,但你正在澆鑄成控制器

如果你想獲得控制器使用大腦這樣

Controller contr = (Controller) factory.getBean("brain"); 
1

控制器對照=(控制器)factory.getBean(「申請」); 您無法將控件對象轉換爲LearnXML對象

試試這個。 LearnXML learnXml =(LearnXML)factory.getBean(「application」); learnXml.getController()。PrintName();

1

更改下面一行代碼

LearnXML learnXML =(LearnXML)factory.getBean(「application」);