Spring依赖注入 第2关:Bean 的属性注入

28 篇文章 9 订阅

目录

任务描述

相关知识

编程要求

测试说明

参考答案


任务描述

本关任务:利用属性注入获取对象。

相关知识

在学习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注入属性

Spring2.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属性注入

Spring3.0提供注入属性方式:

//语法:#{表达式}
<bean id="" value="#{表达式}">
<bean id="car" class="com.entity.Car">
    <property name="name" value="#{'大众'}"></property>
    <property name="price" value="#{'120000'}"></property>
</bean>

编程要求

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

  1. Teacher类和Student类中补充属性的**set**方法;

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

  3. Test类中获取并打印Teacher对象。

测试说明

平台会对你编写的代码进行测试:

预期输出:Teacher [name=张老师, grade=C366, student=Student [name=小吴, sex=男, age=18]]


开始你的任务吧,祝你成功!

参考答案

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 + "]";
	}
}

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 + "]";
	}
}

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) {
        //使用ApplicationContext容器获取对象
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext2.xml");
        Teacher teacher = app.getBean("teacher",Teacher.class);
        //打印对象
        System.out.println(teacher.toString());
	}
}

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="teacher" class="step2.Teacher">
		<property name="name" 	value="张老师"></property>
		<property name="grade" value="C366"></property>
		<property name="student" ref="student"></property>
	</bean>
	<bean id="student" class="step2.Student">
		<property name="name" value="小吴"></property>
		<property name="sex" value="男"></property>
		<property name="age" value="18"></property>
	</bean>
	<!--结束配置  -->
</beans>

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

于建章

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

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

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

打赏作者

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

抵扣说明:

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

余额充值