我完成了Spring IOC容器管理Bean,以及xml方式set注入属性的依赖注入练习。

main
zxn 6 hours ago
parent e131932082
commit 36b26d2da9

@ -0,0 +1,26 @@
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 实验室Lab实体beansetter属性注入 -->
<bean id="lab" class="com.ssm.ioc.Lab">
<property name="labId" value="1001"/>
<property name="labName" value="计算机创新实验室"/>
<property name="labLocation" value="工科楼302"/>
<property name="labManager" value="张老师"/>
<property name="labCapacity" value="50"/>
</bean>
<!-- 材料Material实体beansetter属性注入 -->
<bean id="materialz" class="com.ssm.ioc.Materialz">
<property name="materialId" value="2001"/>
<property name="materialName" value="实验钢材"/>
<property name="materialType" value="金属耗材"/>
<property name="materialStock" value="300"/>
</bean>
</beans>

@ -1,8 +1,8 @@
package ioc;
package com.ssm.ioc;
import java.util.Date;
public class Material {
public class Materialz {
private Integer materialId;
private String materialName;
private String materialType;
@ -10,7 +10,7 @@ public class Material {
private Date inTime;
// Spring必须无参构造
public Material() {
public Materialz() {
}
// getter、setter方法

@ -0,0 +1,24 @@
package com.ssm.ioc;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDi {
public static void main(String[] args) {
// 1. 读取 bean-di.xml 配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("bean-di.xml");
// 2. 获取实验室对象并打印
System.out.println("=== 实验室信息 ===");
Lab lab = (Lab) context.getBean("lab");
System.out.println(lab);
// 3. 获取材料对象并打印
System.out.println("\n=== 材料信息 ===");
Materialz material = (Materialz) context.getBean("materialz");
System.out.println(material);
}
}

@ -0,0 +1,26 @@
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 实验室Lab实体beansetter属性注入 -->
<bean id="lab" class="com.ssm.ioc.Lab">
<property name="labId" value="1001"/>
<property name="labName" value="计算机创新实验室"/>
<property name="labLocation" value="工科楼302"/>
<property name="labManager" value="张老师"/>
<property name="labCapacity" value="50"/>
</bean>
<!-- 材料Material实体beansetter属性注入 -->
<bean id="materialz" class="com.ssm.ioc.Materialz">
<property name="materialId" value="2001"/>
<property name="materialName" value="实验钢材"/>
<property name="materialType" value="金属耗材"/>
<property name="materialStock" value="300"/>
</bean>
</beans>
Loading…
Cancel
Save