现有三个类,学生类( Student ),班级类( Classes ),教师类( Teach ),保存结构如下图 1 图 1 学生类、班级类、教师类结构 学生类结构如下: package com.bean; public class Student { private String name ; private String stuid ; private int age ; public void setName(String name ) { this . name = name ; } public void setStuid(String stuid ) { this . stuid = stuid ; } public void setAge( int age ) { this . age = age ; } public void display(){ System. out .println( " 我是一个学生 :\nname:" + name + "\nstuid:" + stuid + "\nage:" + age + "\n" ); } } 教师类如下: package com.bean; public class Teacher { private String t eachid; private String name ; private int age ; public void setTeachid(String teachid ) { this . teachid = teachid ; } public void setName(String name ) { this . name = name ; } public void setAge( int age ) { this . age = age ; } public void display(){ System. out .println( " 我是一个老师 :\nname:" + name + "\nteachid:" + teachid + "\nage:" + age + "\n" ); } } 班级类如下: package com.bean; public class Classes { private Student stu ; private Teacher teac ; public Student getStu() { return stu ; } public void setStu(Student stu ) { this . stu = stu ; } public Teacher getTeac() { return teac ; } public void setTeac(Teacher teac ) { this . teac = teac ; } public void display(){ stu .display(); teac .display(); } } 运行 Test.Java 程序,结果如图 2 。 图 2 程序运行结果 根据运行结果把下列代码补充完整: Spring.xml
< 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-2.5.xsd" > < bean id = "stu" class = " 1 " > < property name = " 2 " > < value >
< property name = " 3 " > < value > 18
< property name = " 4 " > < value > s1001
< bean id = "teac1" class = " 5 " > < property name = "name" > < value > 丰
< property name = "age" > < value > 45
< property name = "teachid" > < value > T20150901
< bean id = "cs" class = " 6 " > < property name = "stu" > 7
< property name = "teac" > 8
Test.java: package com.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.bean.Classes; import com.bean.Student; import com.bean.Teacher; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext context=new 9 ; Classes cs=( 10 )context.getBean("cs"); cs.display(); } }