feat(di): 实现基于注解的依赖注入功能

- 在 bean-di.xml 中调整 studentName 属性的缩进格式
- 在 bean-di-annotation.xml 中添加组件扫描配置并增加空行
- 修改 bean-ioc.xml 中 department 实体的属性名称
- 新增 Department 注解版实体类,使用 @Component 和 @Value 注解
- 在 TestAnnotation 测试类中添加 department Bean 的获取和测试方法
布鲁特 2 months ago
parent 7961ad22b9
commit f3d6e39c8e

@ -11,4 +11,13 @@
<!-- 开启组件扫描扫描com.ssm.di.annotation包下所有带@Component注解的类 -->
<context:component-scan base-package="com.ssm.di.annotation"/>
</beans>

@ -5,7 +5,7 @@
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.ssm.di.Student">
<property name="studentId" value="2025001"/>
<property name="studentName" value="张三"/>
<property name="studentName" value="张三"/>
<property name="className" value="计科2班"/>
<property name="major" value="计算机科学与技术"/>
<property name="age" value="20"/>

@ -0,0 +1,84 @@
package com.ssm.di.annotation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* DI
* com.ssm.di.annotation
*/
// 1. 标记为Spring容器管理的Bean名称为 "department"
@Component("department")
public class Department {
// 2. 使用@Value注入属性替代XML中的property
@Value("1001")
private Integer deptId; // 院系ID
@Value("计算机学院")
private String deptName; // 院系名称
@Value("CS-2026")
private String deptNo; // 院系编号
@Value("李院长")
private String deptManager; // 院系负责人
@Value("010-88889999")
private String deptPhone; // 联系电话
@Value("主校区")
private String campus; // 所属校区
// 3. 无参构造Spring IoC 实例化必须保留)
public Department() {}
// 4. Getter & Setter (Spring依赖注入必须)
public Integer getDeptId() { return deptId; }
public void setDeptId(Integer deptId) { this.deptId = deptId; }
public String getDeptName() { return deptName; }
public void setDeptName(String deptName) { this.deptName = deptName; }
public String getDeptNo() { return deptNo; }
public void setDeptNo(String deptNo) { this.deptNo = deptNo; }
public String getDeptManager() { return deptManager; }
public void setDeptManager(String deptManager) { this.deptManager = deptManager; }
public String getDeptPhone() { return deptPhone; }
public void setDeptPhone(String deptPhone) { this.deptPhone = deptPhone; }
public String getCampus() { return campus; }
public void setCampus(String campus) { this.campus = campus; }
/**
* 5. printInfo
*/
public void printInfo() {
System.out.println("===== 【院系管理】信息 =====");
System.out.println("院系ID : " + deptId);
System.out.println("院系名称 : " + deptName);
System.out.println("院系编号 : " + deptNo);
System.out.println("负责人 : " + deptManager);
System.out.println("联系电话 : " + deptPhone);
System.out.println("所属校区 : " + campus);
System.out.println("==========================\n");
}
/**
* 6. toString便
*/
@Override
public String toString() {
return "Department{" +
"deptId=" + deptId +
", deptName='" + deptName + '\'' +
", deptNo='" + deptNo + '\'' +
", deptManager='" + deptManager + '\'' +
", deptPhone='" + deptPhone + '\'' +
", campus='" + campus + '\'' +
'}';
}
}

@ -13,5 +13,18 @@ public class TestAnnotation {
// 3. 输出对象信息,验证是否注入成功
System.out.println(student);
// 2. 获取院系Bean (名称对应@Component("department"))
Department department = context.getBean("department", Department.class);
// 3. 调用方法输出结果
System.out.println("=== 调用 printInfo() 方法输出 ===");
department.printInfo();
// 4. 测试 toString() 方法输出
System.out.println("=== 调用 toString() 方法输出 ===");
System.out.println(department);
}
}

@ -56,19 +56,11 @@
<bean id="department" class="com.ssm.ioc.Department">
<property name="deptId" value="1001"/>
<property name="deptName" value="计算机科学与技术系"/>
<property name="deptCode" value="CS001"/>
<property name="college" value="信息工程学院"/>
<property name="dean" value="张教授"/>
<property name="phone" value="13800138000"/>
<property name="deptNo" value="CS001"/>
<property name="deptManager" value="张教授"/>
<property name="deptPhone" value="13800138000"/>
<property name="campus" value="信息工程学院"/>
</bean>
<!-- 可补充其他院系实例,用于测试 -->
<bean id="department2" class="com.ssm.ioc.Department">
<property name="deptId" value="1002"/>
<property name="deptName" value="电子信息工程系"/>
<property name="deptCode" value="EE001"/>
<property name="college" value="信息工程学院"/>
<property name="dean" value="李教授"/>
<property name="phone" value="13900139000"/>
</bean>
</beans>
Loading…
Cancel
Save