Spring依赖注入 第3关:Bean 的构造函数注入

28 篇文章 9 订阅

目录

任务描述

相关知识

编程要求

测试说明

参考答案


任务描述

本关任务:利用构造函数注入获取对象。

相关知识

为了完成本关任务,你需要掌握:怎样使用构造函数注入。

构造函数注入

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]

编程要求

查看右侧文件夹,完成以下任务:

  1. Teacher类和Student类中补充构造方法

  2. Spring的配置文件applicationContext3.xml补充完整;

  3. 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());
	}
}

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

于建章

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值