springMVC 学习(什么是依赖注入)

上一篇博文中,我们学习了springIOC,又称spring控制反转,即将对象的创建销毁等操作交给spring容器来处理,今天学习spring的依赖注入,那么什么是依赖注入,说的通俗一点,就是对属性赋值,也就是说我们利用spring来为我们的类中包含的属性来进行赋值,想想之前我们是通过这样的方式来编写代码的:接口 对象 = new 接口实现类(); 再看看我们之前是怎么给属性赋值的

1.通过set方法

2.通过构造方法

今天我们来实现通过spring依赖注入来为类中的变量赋值。首先我新建一个Student.java和一个Teacher.java类,并且提供get和set方法

package com.test.di;public class Student {private String name;private int id;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}}package com.test.di;public class Teacher {private String teacherName;private Student student;public String getTeacherName() {return teacherName;}public void setTeacherName(String teacherName) {this.teacherName = teacherName;}public Student getStudent() {return student;}public void setStudent(Student student) {this.student = student;}} 然后,我们在spring配置文件中来为这些属性赋值:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi=""xsi:schemaLocation=""><bean id="student" class="com.test.di.Student"><property name="name" value="haha"></property><property name="id" value="12"></property></bean><bean id="teacher" class="com.test.di.Teacher"><property name="teacherName" value="teacherWang"></property><property name="student" ref="student"></property></bean></beans> 根据配置文件,我们可以发现,在bean中有个property的配置,其中name就是我要为那个属性赋值,对于属性的值,这里有两种情况:

1.如果是基本类型,直接在value中写上需要赋的值即可

2.如果是引用类型,那么需要使用ref来引用对应的类,对于这个栗子,即student这里ref所引用的student就是第一个student的bean中配置的id。

接下来,我编写一个测试类,来测试是否成功的为属性注入对应的值DiTest.java

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/test/di/applicationContext.xml");Teacher teacher = (Teacher) applicationContext.getBean("teacher");Student student = teacher.getStudent();System.out.println(teacher.getTeacherName());System.out.println("studentName :"+student.getName()+"==studentId :"+student.getId());此时打印结果如下:

teacherWangstudentName :haha==studentId :12

发现这个时候spring容器已经为我们的属性赋值成功了。然而我们却并没有像之前那样调用set方法,或者是构造方法,这里有一点需要说明,就是我们虽然没有自己调用set方法来为属性赋值,但是spring还是会掉用set方法,所以我们如果想对某一个属性进行依赖注入的话,那么我们就需要对该属性写上set方法。

下面我们为teacher注入一些集合,首先需要做的就是在Teacher.java中声明list,set,map这三个属性,然后为这些属性生成set方法,新增属性如下:

private List<String>lists;private Set<Integer>sets;private Map<Integer,String>maps;然后再spring的配置文件中这样为其赋值:

<bean id="teacher" class="com.test.di.Teacher"><property name="teacherName" value="teacherWang"></property><property name="student" ref="student"></property><property name="lists"><list><value>one</value><value>two</value><value>three</value></list></property><property name="maps"><map><entry key="1" value="firstMap"></entry><entry key="2" value="secondMap"></entry><entry key="3" value="thirdMap"></entry></map></property><property name="sets"><set><value>111</value><value>222</value><value>333</value></set></property></bean> 可以发现这个配置文件写起来和普通的集合对象的形式是很相似的,这里我们都是用的基本的类型来作为集合的泛型,如果使用的是引用类型,这里的配置都有ref对应的属性,只需要将所需要引用的类对象的id写入到ref的值当中即可,举个栗子:

对于list和set如果泛型是引用类型,那么可以这样写:

<ref bean=""/>

而对于map如果类型是引用类型,可以这样写:

<entry key-ref="" value-ref=""></entry>

好了,是时候验证是否赋值成功了。IocTest.java

筑起梦想的鸟巢,开始人生的长跑,领先每回的冲刺,

springMVC 学习(什么是依赖注入)

相关文章:

你感兴趣的文章:

标签云: