目录
任务描述
本关任务:利用构造函数注入获取对象。
相关知识
为了完成本关任务,你需要掌握:怎样使用构造函数注入。
构造函数注入
constructor-arg
标签表示构造函数注入,在Spring4.x
版本后constructor-arg
标签是没有name
属性的,ref
属性值的含义和属性注入一样,会把对象作为构造函数的参数注入到调用者对象内,index
属性对应的值是构造函数参数的下标(index
属性可省略不写效果一样):
<bean id="stu" class="com.entity.Student">
<constructor-arg index="0" value="小吴"></constructor-arg>
<constructor-arg index="1" value="18"></constructor-arg>
<bean>
使用构造函数注入方式的前提是类文件中必须要有构造方法:
public class Student {
private String name;
private int age;
private double score;
public Student(String name,int age) {
super();
this.name = name;
this.age=age;
}
}
但是使用该方式会存在问题,比如如下情况:
//Student类中的另一个重载构造函数
public Student(String name, double score) {
super();
this.name = name;
this.score = score;
}
此时若仍使用index
属性程序则不知道执行哪个构造函数,因此使用type
属性可以用来区分,且这些属性之间是可以混合使用的。 如配置第二个构造函数:
<bean id="stu" class="com.entity.Student">
<constructor-arg value="小花"></constructor-arg>
<constructor-arg type="double" value="89.0"></constructor-arg>
</bean>
输出为: Student [name=小花, age=0, score=89.0]
编程要求
查看右侧文件夹,完成以下任务:
-
在
Teacher
类和Student
类中补充构造方法; -
将
Spring
的配置文件applicationContext3.xml
补充完整; -
在
Test
类中获取并打印Teacher
对象
测试说明
平台会对你编写的代码进行测试:
预期输出:Teacher [name=张老师, grade=C366, student=Student [name=小花, age=18, score=89.0]]
开始你的任务吧,祝你成功!
参考答案
Student.java
package step3;
public class Student {
private String name;
private int age;
private double score;
/****** Begin ******/
Student(String name,int age,double score){
this.name=name;
this.age=age;
this.score=score;
}
/****** End ******/
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", score=" + score+ "]";
}
}
Teacher.java
package step3;
public class Teacher {
private String name;
private String grade;//学生年级
private Student student;
/****** Begin ******/
Teacher(String name,String grade,Student student){
this.name=name;
this.grade=grade;
this.student=student;
}
/****** End ******/
@Override
public String toString() {
return "Teacher [name=" + name + ", grade=" + grade + ", student="+ student + "]";
}
}
applicationContext3.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="teacher" class="step3.Teacher">
<constructor-arg value="张老师"></constructor-arg>
<constructor-arg value="C366"></constructor-arg>
<constructor-arg ref="student"></constructor-arg>
</bean>
<bean id="student" class="step3.Student">
<constructor-arg value="小花"></constructor-arg>
<constructor-arg value="18"></constructor-arg>
<constructor-arg value="89.0"></constructor-arg>
</bean>
<!--结束配置 -->
</beans>
Test.java
package step3;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//使用ApplicationContext容器获取对象
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext3.xml");
Teacher t = app.getBean("teacher",Teacher.class);
//打印对象
System.out.println(t.toString());
}
}