创建di软件包,创建测试类和User类

main
wangcong 3 weeks ago
parent 2ec3fd8b2f
commit 1c0c5895d8

@ -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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- ====================================== -->
<!-- 组长负责用户信息维护模块User -->
<!-- 采用作业要求的setter方式注入属性 -->
<!-- ====================================== -->
<bean id="user" class="com.ssm.di.User">
<!-- 注入用户ID -->
<property name="id" value="1"/>
<!-- 注入用户名 -->
<property name="username" value="zhangsan"/>
<!-- 注入登录密码 -->
<property name="userpass" value="123456"/>
<!-- 注入真实姓名 -->
<property name="truename" value="张三"/>
<!-- 注入联系电话 -->
<property name="phone" value="13800138000"/>
<!-- 注入注册时间 -->
<property name="createTime" value="2024-01-01"/>
</bean>
</beans>

@ -25,7 +25,7 @@
</bean>
<!-- 组员A收入信息维护 -->
<bean id="income" class="com.ssm.ioc.Income">
<property name="id" value="1001"/>
<property name="userId" value="10"/>
@ -37,7 +37,7 @@
<property name="remark" value="工资收入"/>
</bean>
<!-- 组员B支出信息维护 -->
<bean id="expense" class="com.ssm.ioc.Expense">
<property name="id" value="2001"/>
<property name="userId" value="10"/>

@ -0,0 +1,17 @@
package com.ssm.di;
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 ac = new ClassPathXmlApplicationContext("bean-di.xml");
// 2. 获取User Bean
User user = (User) ac.getBean("user");
// 3. 调用printInfo方法输出信息作业要求
user.printInfo();
}
}

@ -0,0 +1,38 @@
package com.ssm.di; // 确保包名一致
public class User {
// 属性定义
private Integer id;
private String username;
private String userpass;
private String truename;
private String phone;
private String createTime;
// 1. 必须有无参构造 (Spring反射必备)
public User() {}
// 2. 核心方法printInfo (作业要求)
public void printInfo() {
System.out.println("用户ID: " + id);
System.out.println("用户名: " + username);
System.out.println("真实姓名: " + truename);
System.out.println("手机号: " + phone);
System.out.println("注册时间: " + createTime);
}
// 3. Getter & Setter (必须提供用于Set注入)
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getUsername() { return username; }
public void setUsername(String username) { this.username = username; }
public String getUserpass() { return userpass; }
public void setUserpass(String userpass) { this.userpass = userpass; }
public String getTruename() { return truename; }
public void setTruename(String truename) { this.truename = truename; }
public String getPhone() { return phone; }
public void setPhone(String phone) { this.phone = phone; }
public String getCreateTime() { return createTime; }
public void setCreateTime(String createTime) { this.createTime = createTime; }
}

@ -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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- ====================================== -->
<!-- 组长负责用户信息维护模块User -->
<!-- 采用作业要求的setter方式注入属性 -->
<!-- ====================================== -->
<bean id="user" class="com.ssm.di.User">
<!-- 注入用户ID -->
<property name="id" value="1"/>
<!-- 注入用户名 -->
<property name="username" value="zhangsan"/>
<!-- 注入登录密码 -->
<property name="userpass" value="123456"/>
<!-- 注入真实姓名 -->
<property name="truename" value="张三"/>
<!-- 注入联系电话 -->
<property name="phone" value="13800138000"/>
<!-- 注入注册时间 -->
<property name="createTime" value="2024-01-01"/>
</bean>
</beans>

@ -25,7 +25,7 @@
</bean>
<!-- 组员A收入信息维护 -->
<bean id="income" class="com.ssm.ioc.Income">
<property name="id" value="1001"/>
<property name="userId" value="10"/>
@ -37,7 +37,7 @@
<property name="remark" value="工资收入"/>
</bean>
<!-- 组员B支出信息维护 -->
<bean id="expense" class="com.ssm.ioc.Expense">
<property name="id" value="2001"/>
<property name="userId" value="10"/>

Loading…
Cancel
Save