diff --git a/equipment-SpringProject/equipment-SpringProject.iml b/equipment-SpringProject/equipment-SpringProject.iml index 52714a6..bb2779b 100644 --- a/equipment-SpringProject/equipment-SpringProject.iml +++ b/equipment-SpringProject/equipment-SpringProject.iml @@ -9,5 +9,6 @@ + \ No newline at end of file diff --git a/equipment-SpringProject/src/bean-di-annotation.xml b/equipment-SpringProject/src/bean-di-annotation.xml new file mode 100644 index 0000000..0efcc3f --- /dev/null +++ b/equipment-SpringProject/src/bean-di-annotation.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/equipment-SpringProject/src/com/ssm/di/annotation/Course.java b/equipment-SpringProject/src/com/ssm/di/annotation/Course.java new file mode 100644 index 0000000..71e0723 --- /dev/null +++ b/equipment-SpringProject/src/com/ssm/di/annotation/Course.java @@ -0,0 +1,39 @@ +package com.ssm.di.annotation; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +@Component +public class Course { + private String courseId; + private String courseName; + private int credit; + + // 自动注入Teacher对象(核心注解) + @Autowired + private Teacher teacher; + + // 无参构造 + 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; + } + + // 输出方法,和之前XML版本的格式完全一致 + 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/annotation/Teacher.java b/equipment-SpringProject/src/com/ssm/di/annotation/Teacher.java new file mode 100644 index 0000000..45d7d80 --- /dev/null +++ b/equipment-SpringProject/src/com/ssm/di/annotation/Teacher.java @@ -0,0 +1,40 @@ +package com.ssm.di.annotation; + +import org.springframework.stereotype.Component; + +// 让Spring自动扫描并创建这个Bean +@Component +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/annotation/TestAnnotation.java b/equipment-SpringProject/src/com/ssm/di/annotation/TestAnnotation.java new file mode 100644 index 0000000..2524436 --- /dev/null +++ b/equipment-SpringProject/src/com/ssm/di/annotation/TestAnnotation.java @@ -0,0 +1,11 @@ +package com.ssm.di.annotation; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class TestAnnotation { + public static void main(String[] args) { + ApplicationContext ac = new ClassPathXmlApplicationContext("bean-di-annotation.xml"); + Course course = ac.getBean("course", Course.class); + course.printInfo(); + } +} \ No newline at end of file diff --git a/equipment-SpringProject/src/libs/spring-aop-5.1.6.RELEASE.jar b/equipment-SpringProject/src/libs/spring-aop-5.1.6.RELEASE.jar new file mode 100644 index 0000000..35f580e Binary files /dev/null and b/equipment-SpringProject/src/libs/spring-aop-5.1.6.RELEASE.jar differ