极速版
- src/step2/Student.java
-
package step2; public class Student { private String name; private String sex; private int age; /****** Begin ******/ public void setName(String name){ this.name = name; } public void setSex(String sex){ this.sex = sex; } public void setAge(int age){ this.age = age; } /****** End ******/ @Override public String toString() { return "Student [name=" + name + ", sex=" + sex + ", age=" + age + "]"; } }
- src/step2/Teacher.java
-
package step2; public class Teacher { private String name; private String grade;//学生年级 private Student student; /****** Begin ******/ public void setName(String name){ this.name = name; } public void setGrade(String grade){ this.grade = grade; } public void setStudent(Student student){ this.student = student; } /****** End ******/ @Override public String toString() { return "Teacher [name=" + name + ", grade=" + grade + ", student=" + student + "]"; } }
- src/step2/Test.java
-
package step2; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import step2.Teacher; public class Test { public static void main(String[] args) { /********** Begin **********/ //使用ApplicationContext容器获取对象 ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext2.xml"); Teacher teacher = (Teacher) app.getBean("teacher"); //打印对象 System.out.println(teacher); /********** End **********/ } }
- src/step2/applicationContext2.xml
-
<?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"> <!--开始配置bean --> <bean id="student" class="step2.Student"> <property name="name" value="小吴"></property> <property name="sex" value="男"></property> <property name="age" value="18"></property> </bean> <bean id="teacher" class="step2.Teacher"> <property name="name" value="张老师"></property> <property name="grade" value="C366"></property> <property name="student" ref="student"></property> </bean> <!--结束配置 --> </beans>
以下题目
-
任务描述
本关任务:利用属性注入获取对象。相关知识
在学习 Spring 的依赖注入( DI )前,我们必须了解 DI 和 IOC 的区别是什么?IOC(控制反转):将对象的创建权,由 Spring 管理。
DI(依赖注入):在 Spring 创建对象的过程中,把对象依赖的属性注入到类中。
下面我们就一起来学习如何将属性注入到类中。
课程视频 - 注入普通属性
属性注入
property 标签表示属性注入,属性注入是指 IOC 容器通过成员变量的 setter 方法来注入被依赖对象。配置文件中的 name 属性必须和成员变量属性名称一致,系统会把此对象作为 setXXX() 的参数,传入到调用者。<bean id="student" class="com.entity.Student">
<property name="name" value="小明"></property>
</bean>
当有需要关联 bean 时,我们可以使用 ref 属性,值就是你所需要关联的 bean 对象的 id,我们对applicationContext.xml文件进行如下修改:<bean id="student" class="com.entity.Student">
<property name="name" value="小明"></property>
<property name="classInfo" ref="info"></property>
</bean>
<bean id="info" class="com.entity.ClassInfo">
<property name="classs" value="软件1631班"></property>
</bean>
使用属性注入方式的前提是类文件中必须有 setXXX() 方法:public class Student {
private String name;
private ClassInfo classInfo;
public void setName(String name) {
this.name = name;
}
public void setClassInfo(ClassInfo classInfo) {
this.classInfo = classInfo;
}
@Override
public String toString() {
return "Student [name=" + name + ", classInfo=" + classInfo + "]";
}
}
public class ClassInfo {
private String classs;
public void setClasss(String classs) {
this.classs = classs;
}
@Override
public String toString() {
return "ClassInfo [classs=" + classs + "]";
}
}
//测试类
public class Test {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
Student stu = (Student) app.getBean("student");
System.out.println(stu);
}
}
输出结果:
Student [name=小明, classInfo=ClassInfo [classs=软件1631班]]其他属性注入方式
课程视频 - 注入对象属性
名称空间 p 注入属性
Spring 2.5 版本引入了名称空间 p :p:<属性名>="xxx" //引入常量值
p:<属性名>-ref="xxx" //引用其它Bean对象
使用前需先引入名称空间:<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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">
<bean id="car" class="com.entity.Car" p:name="宝马" p:price="400000"/>
<bean id="person" class="com.entity.Person" p:name="童童" p:car2-ref="car"/>
</beans>课程视频 - 注入复杂属性
SpEL 属性注入
Spring 3.0 提供注入属性方式://语法:#{表达式}
<bean id="" value="#{表达式}">
<bean id="car" class="com.entity.Car">
<property name="name" value="#{'大众'}"></property>
<property name="price" value="#{'120000'}"></property>
</bean>
编程要求
查看右侧文件夹,完成以下任务:在 Teacher 类和 Student 类中补充属性的set方法;
将 Spring 的配置文件 applicationContext2.xml 补充完整;
在 Test 类中获取并打印 Teacher 对象。
测试说明
平台会对你编写的代码进行测试:预期输出:
Teacher [name=张老师, grade=C366, student=Student [name=小吴, sex=男, age=18]]