代宇航编写初始代码

王健旭完善初始代码
喻廷屿检查代码
孙佳兴运行并测试
赵文博进行收尾并上传
main
your-name 2 months ago
parent 01739066dc
commit 3f22cb29fa

@ -0,0 +1,22 @@
<?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">
<!-- Teacher Bean注入工号、姓名、职称 -->
<bean id="teacher" class="com.ssm.di.xml.Teacher">
<property name="teacherId" value="T001"/>
<property name="name" value="张教授"/>
<property name="title" value="教授"/>
</bean>
<!-- Course Bean注入课程编号、名称、学分和Teacher对象 -->
<bean id="course" class="com.ssm.di.xml.Course">
<property name="courseId" value="C001"/>
<property name="courseName" value="Java程序设计"/>
<property name="credit" value="4"/>
<property name="teacher" ref="teacher"/>
</bean>
</beans>

@ -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());
}
}

@ -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;
}
}

@ -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();
}
}
Loading…
Cancel
Save