diff --git a/equipment-SpringProject/src/bean-di-xml.xml b/equipment-SpringProject/src/bean-di-xml.xml new file mode 100644 index 0000000..e2b8fc2 --- /dev/null +++ b/equipment-SpringProject/src/bean-di-xml.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/equipment-SpringProject/src/com/ssm/di/xml/Course.java b/equipment-SpringProject/src/com/ssm/di/xml/Course.java new file mode 100644 index 0000000..a3ff754 --- /dev/null +++ b/equipment-SpringProject/src/com/ssm/di/xml/Course.java @@ -0,0 +1,36 @@ +package com.ssm.di.xml; + +public class Course { + private String courseId; // 课程编号 + private String courseName; // 课程名称 + private int credit; // 学分 + private Teacher teacher; // 授课教师 + + // Spring 必须的无参构造 + public Course() {} + + // Setter 方法(供XML注入使用) + public void setCourseId(String courseId) { + this.courseId = courseId; + } + + public void setCourseName(String courseName) { + this.courseName = courseName; + } + + public void setCredit(int credit) { + this.credit = credit; + } + + public void setTeacher(Teacher teacher) { + this.teacher = teacher; + } + + // 修改为和第一张截图完全一致的输出格式 + public void printInfo() { + System.out.println("课程编号:" + courseId); + System.out.println("课程名称:" + courseName); + System.out.println("学分:" + credit); + System.out.println("授课教师信息:工号:" + teacher.getTeacherId() + ",姓名:" + teacher.getName() + ",职称:" + teacher.getTitle()); + } +} \ No newline at end of file diff --git a/equipment-SpringProject/src/com/ssm/di/xml/Teacher.java b/equipment-SpringProject/src/com/ssm/di/xml/Teacher.java new file mode 100644 index 0000000..9756288 --- /dev/null +++ b/equipment-SpringProject/src/com/ssm/di/xml/Teacher.java @@ -0,0 +1,36 @@ +package com.ssm.di.xml; + +public class Teacher { + private String teacherId; // 工号 + private String name; // 姓名 + private String title; // 职称 + + // Spring 必须的无参构造 + public Teacher() {} + + // Setter 方法(供XML注入使用) + public void setTeacherId(String teacherId) { + this.teacherId = teacherId; + } + + public void setName(String name) { + this.name = name; + } + + public void setTitle(String title) { + this.title = title; + } + + // Getter 方法(供Course调用) + public String getTeacherId() { + return teacherId; + } + + public String getName() { + return name; + } + + public String getTitle() { + return title; + } +} \ No newline at end of file diff --git a/equipment-SpringProject/src/com/ssm/di/xml/TestDI.java b/equipment-SpringProject/src/com/ssm/di/xml/TestDI.java new file mode 100644 index 0000000..b2eb32d --- /dev/null +++ b/equipment-SpringProject/src/com/ssm/di/xml/TestDI.java @@ -0,0 +1,17 @@ +package com.ssm.di.xml; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class TestDI { + public static void main(String[] args) { + // 1. 加载Spring配置文件 + ApplicationContext context = new ClassPathXmlApplicationContext("bean-di-xml.xml"); + + // 2. 获取Course对象 + Course course = context.getBean("course", Course.class); + + // 3. 调用方法,输出信息 + course.printInfo(); + } +} \ No newline at end of file diff --git a/out/production/equipment-SpringProject/com/ssm/first/TestFirst.class b/out/production/equipment-SpringProject/com/ssm/first/TestFirst.class index c8b7c03..9db8114 100644 Binary files a/out/production/equipment-SpringProject/com/ssm/first/TestFirst.class and b/out/production/equipment-SpringProject/com/ssm/first/TestFirst.class differ