代宇航修改初始代码

王健旭进一步完善代码
喻廷屿检查代码
孙佳兴运行并测试
赵文博进行收尾并上传
main
your-name 3 weeks ago
parent 3f22cb29fa
commit 4445211a01

@ -9,5 +9,6 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="commons-logging-1.1.1" level="project" />
<orderEntry type="library" name="libs" level="project" />
<orderEntry type="library" name="spring-aop-5.1.6.RELEASE" level="project" />
</component>
</module>

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启组件扫描Spring会自动扫描@注解 -->
<context:component-scan base-package="com.ssm.di.annotation"/>
<!-- 给Teacher属性赋值 -->
<bean id="teacher" class="com.ssm.di.annotation.Teacher">
<property name="teacherId" value="T001"/>
<property name="name" value="张教授"/>
<property name="title" value="教授"/>
</bean>
<!-- 给Course属性赋值teacher已经由@Autowired自动注入 -->
<bean id="course" class="com.ssm.di.annotation.Course">
<property name="courseId" value="C001"/>
<property name="courseName" value="Java程序设计"/>
<property name="credit" value="4"/>
</bean>
</beans>

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

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

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