> 文章列表 > 依赖注入~

依赖注入~

依赖注入~

依赖注入之setter注入:

依赖注入是IOC具体的一种实现方式, 这是针对资源获取的方式角度来说的,之前我们是被动接受,现在IOC具体的实现叫做依赖注入,从代码的角度来说,原来创建对象的时候需要new,而现在并不需要这样做,只需要将所依赖的对象给它设置相对应的方法,set方法也行,有参构造也行,以我们设置好的方法来接受spring为我们所注入的对象

比如,我们此时创建一个实体类,其中包含id,age,name等属性,那我们就说student对象是依赖于id/name/age这些属性的,既然student依赖于这些属性,那么我们就可以在IOC容器中为其进行赋值,依赖注入通俗点说就是为类中的属性赋值的过程

student类:

package xysfxy;public class student implements person {private  Integer sid;private  String sname;private  Integer age;private  String gender;public student() {}public Integer getSid() {return sid;}public void setSid(Integer sid) {this.sid = sid;}public String getSname() {return sname;}public Integer getAge() {return age;}public void setAge(Integer age) {this.age = age;}public void setSname(String sname) {this.sname = sname;}public String getGender() {return gender;}public void setGender(String gender) {this.gender = gender;}@Overridepublic String toString() {return "studnet{" +"sid=" + sid +", sname=" + sname +", age=" + age +", gender='" + gender + '\\'' +'}';}public student(Integer sid, String name, Integer age, String  gender ) {this.age=age;this.sname=sname;this.gender=gender;this.sid = sid;}
}

如下所示,当我们为student类中的name属性赋值,注意:name在这里是属性,那么什么叫属性呢?找到当前类中set和get方法,把方法中的set和get去掉,剩余部分的首字母变成小写的结果就是属性,由于我们当前是给属性赋值,因此需要寻找set方法

依赖注入~
在spring.xml文件中为属性赋值:

<!-- property:通过成员变量的set方法进行赋值,name:设置需要赋值的属性名(和set方法有关),value:设置为属性所赋的值-->
<bean id="student1" class="xysfxy.student"  ><property name="sid" value="1001"></property><property name="sname" value="张三"></property><property name="age" value="20"></property><property name="gender" value=""></property>
</bean>

创建测试类:

import org.junit.Test;
import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;
import xysfxy.student;public class student_text {@Testpublic void test(){ApplicationContext ioc=new ClassPathXmlApplicationContext("applicantsContext.xml");student student= ioc.getBean(student.class);System.out.println(student);}
}

输出如下所示:

studnet{sid=1001, sname=张三, age=20, gender='男'}

此时输出的结果正是我们上述所赋的值

依赖注入之构造器注入:

在spring.xml文件中赋值:

<!-- 赋值时,一定要与构造器中的属性相对应-->
<bean id="student1" class="xysfxy.student"  ><constructor-arg  value="1002"></constructor-arg><constructor-arg  value="李四"></constructor-arg><constructor-arg  value="19"></constructor-arg><constructor-arg  value=""></constructor-arg>
</bean>

其他不变,运行结果如下所示:

studnet{sid=1002, sname=李四, age=19, gender='男'}

在上述student类中,添加score属性,并创建新的有参构造:

//新的有参构造中使用score属性代替age属性
public student(Integer sid, String name, String  gender , double score) {this.sid = sid;this.sname=name;this.gender=gender;this.score=score;}//原有参构造public student(Integer sid, String name, String  gender, Integer age ) {this.sid = sid;this.sname=name;this.gender=gender;this.age=age;
}//重写tostring方法@Override
public String toString() {return "student{" +"sid=" + sid +", sname='" + sname + '\\'' +", age=" + age +", gender='" + gender + '\\'' +", score=" + score +'}';
}//为新添加的属性设置set和get方法public double getScore() {return score;}public void setScore(double score) {this.score = score;}

spring.xml文件中的参数设置不变:

<bean id="student1" class="xysfxy.student"  ><constructor-arg  value="1002"></constructor-arg><constructor-arg  value="李四"></constructor-arg><constructor-arg  value=""></constructor-arg><constructor-arg  value="24"></constructor-arg></bean>

在当有参构造器中包含参数相同的情况下,spring.xml文件中的第四个数据24既可以匹配给整形age,也可以匹配给double类型的score,测试结果如下:

依赖注入~

我们发现默认情况下,24匹配给了score,但事实我们是想实现将24匹配给年龄, 那么该如何实现呢?

关于这种情况网上的解决方法有很多:

 方法1:spring.xml文件的bean标签的最后一个属性值设置时,指定name="age"
 方法2:将有参构造中包含age的那个构造方法放在另一个的前面

我使用第二种方式可以,但第一种就会报错,至于为什么不能两个方法通用,我也很疑惑…

依赖注入之特殊值处理:

字面量赋值:

什么是字面量?

例如:

int a=10;

声明一个变量a,初始化为10,此时a就不代表字母a了,而是作为一个变量的名字,当我们引用a的时候,我们实际上拿到的值是10,而如果a是带引号的:“a”,那么它现在不是一个变量,它就是代表a这个字母本身,这就是字面量,所以字面量没有引申含义,就是我们看到的这个数据本身

举例:

我们在spring.xml文件中设置gender属性的值为"null":

<bean id="student1" class="xysfxy.student"  ><property name="sname" value="张三" ></property><property name="sid" value="1002" ></property><property name="gender" value="null"> </property><property name="age" value="24" ></property></bean>

输出如下所示:

student{sid=1002, sname='李四', age=24, gender='null', score=0.0}

我们发现,gender最终输出的值就是null,那他表示的是null对象还是值为null的字符串呢?

我们可通过下述代码进行测试,如果没有报错,则证明当前的null为值是null的字符串:

System.out.println(student.getGender().toString());

输出如下所示:

依赖注入~

如果它是null对象的话,应该报错为空指针异常

正确的将属性设置为null对象的方法如下:

<bean id="student1" class="xysfxy.student"  ><property name="sname" value="张三" ></property><property name="sid" value="1002" ></property><!-- 将gender属性设置为null对象,使用null标签表示,由于该标签中的内容是空,因此我们可以写为单标签 --><property name="gender"><null/></property>
</bean>

输出如下:

依赖注入~

xml实体:

小于号在XML文档中用来定义标签的开始,不能随便使用,如下所示:

我们想输出name的值是一个带"<>"的,如果直接添加带符号在XML文档是不可行的

依赖注入~

解决方案:使用XML实体来代替,&lt;表示小于号,&gt;表示大于号,修改上述代码如下所示:

 <property name="sname" value="&lt;张三&gt;" ></property>

输出结果如下所示:

此时的sname的值中的大括号和小括号都被显示出来

student{sid=1002, sname='<张三>', age=11, gender='null', score=0.0}

关于特殊符号无法被解析的问题,我们还有另外一种办法,就是使用CDATA节CDATA中的C代表Character,是文本,字符的含义,CDATA就代表纯文本数据,XML解析器看到CDATA就知道这里是纯文本,就不会当做XML标签或属性来解析,所以CDATA节中写什么都任意,在IDE中,我们可以通过快捷键使用这个功能,CD[一定要大写],回车即可,如下所示:

 <property name="sname" ><value><![CDATA[<张三>]]></value></property>

输出如下:

student{sid=1002, sname='<张三>', age=11, gender='null', score=0.0}

这种方式也可以正确的显示特殊字符,但需要注意的是它是XML中一个特殊的标签,我们不能将其当做value属性的值写在value属性的后面

依赖注入之为类类型的属性赋值:

创建实体类班级:

package poij;public class clazz {private  Integer cid;private  String name;public Integer getCid() {return cid;}public void setCid(Integer cid) {this.cid = cid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public clazz(Integer cid,String name) {this.cid = cid;this.name=name;}public clazz() {}@Overridepublic String  toString() {return "clazz{" +"cid=" + cid +", name='" + name + '\\'' +'}';}
}

修改student类:

//班级和学生之间是一对多的关系,将班级作为学生的一个属性加入到学生类中private  clazz clazz;//并为其设置set和get方法public poij.clazz getClazz() {return clazz;}public void setClazz(poij.clazz clazz) {this.clazz = clazz;}//重写toString方法@Overridepublic String toString() {return "student{" +"sid=" + sid +", sname='" + sname + '\\'' +", age=" + age +", gender='" + gender + '\\'' +", clazz=" + clazz +'}';}

那么对于student类来说,clazz属性该如何赋值呢?

我们不能使用value对其进行赋值,因为value是给字面量赋值的,但clazz对应的是一个对象

那么该如何给类类型赋值呢?

引用外部的bean给类类型赋值:

修改spring文件中的代码:

<!-- 在给类为student的bean对象的clazz赋值时,使用引用外部bean的方式
ref:引用IOC容器中的某个bean-->
<property name="clazz" ref="clazz1"></property><!-- 创建类型为clazz的bean对象-->
<bean id="clazz1" class="poij.clazz"><property name="cid" value="1111"></property><property name="name" value="菜鸟班"></property>
</bean>

输出如下所示:

依赖注入~

通过级联方式给类类型赋值:

在mybatis中,我们也曾经使用过级联的方式为其赋值,也就是通过类.属性的方式对其进行赋值,那么在spring中,也可以使用该方法吗?

如下所示:尝试使用类.属性的方式直接赋值

<property name="clazz.cid" value="2222"></property>
<property name="clazz.name" value="卷心菜班"></property>

运行报错:虽然上述这种方法可以在mybatis中使用,但是在spring中是不行的

依赖注入~

使用级联方式的前提条件是:要保证提前为clazz属性赋值或者实例化

方法1:先赋值再修改

<bean id="student1" class=" poij.student"  ><property name="sid" value="1001"></property><property name="sname" value="张三"></property><property name="age" value="20"></property><property name="gender" value=""></property><!-- 先通过引用外部bean的方式为其赋值 --><property name="clazz" ref="clazz1"></property><!-- 再对上述 引用的bean对象的值进行修改--><property name="clazz.cid" value="2222"></property><property name="clazz.name" value="卷心菜班"></property></bean><!-- 创建外部的bean对象--> 
<bean id="clazz1" class="poij.clazz"><property name="cid" value="1111"></property><property name="name" value="菜鸟班"></property>
</bean>

方法2:在student类中对clazz进行实例化

private  clazz clazz= new clazz( );

在spring.xml文件中可直接通过类.属性的方式进行赋值:

<property name="clazz.cid" value="2222"></property>
<property name="clazz.name" value="卷心菜班"></property>

上述任意一种,输出均为如下所示:

student{sid=1001, sname='张三', age=20, gender='男', clazz=clazz{cid=2222, name='卷心菜班'}}

虽然以级联的方式也可以对类类型进行赋值,但这种方法我们并不常用,我们通常使用内部bean的方式,下面我们就具体学习一下该方式如何使用:

使用内部bean的方式为类类型赋值:

修改spring.xml中的bean标签:

<bean id="student1" class=" poij.student"  ><property name="sid" value="1001"></property><property name="sname" value="张三"></property><property name="age" value="20"></property><property name="gender" value=""></property><!-- 内部bean的方式为:通过设置一个内部的bean对象而为clazz对象进行赋值--><property name="clazz"><!-- 将为班级赋值的过程写在一个内部的bean标签中--><bean id="clazz_inner" class="poij.clazz"><property name="cid" value="333"></property><property name="name" value="卷王班"></property></bean></property>
</bean>

输出如下所示:

student{sid=1001, sname='张三', age=20, gender='男', clazz=clazz{cid=333, name='卷王班'}}

内部bean就类似于我们在java中学过的内部类一样,它只能在bean的内部进行使用,那么内部bean可以通过IOC容器进行获取吗?

修改测试类中的代码:

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import poij.clazz;public class student_text {@Testpublic void test(){ApplicationContext ioc=new ClassPathXmlApplicationContext( "spring.xml");//尝试获取内部beanclazz clazz1= ioc.getBean( clazz.class);System.out.println(clazz1);}
}

输出如下所示:

看来内部bean确实无法通过IOC容器直接获取

依赖注入~

说到这里,为类类型的属性赋值的三种方式我们已经说完了,引用外部的bean以及使用内部bean是我们所推荐的方法,第二种使用级联并不是我们所推荐的,原因是,它需要先赋值再修改,这样好像并不是真正意义上的赋值,而是修改已有的值

依赖注入之为数组类型的属性赋值:

修改student类中的代码:

//添加新的属性
private  String [] hobby;//为新的属性编写set和get方法
public String[] getHobby() {return hobby;}public void setHobby(String[] hobby) {this.hobby = hobby;}
//重写toString方法@Overridepublic String toString() {return "student{" +"sid=" + sid +", sname='" + sname + '\\'' +", age=" + age +", gender='" + gender + '\\'' +", clazz=" + clazz +", hobby=" + Arrays.toString(hobby) +'}';}

在spring.xml中为hobby属性赋值:

<bean id="student1" class=" poij.student"  ><property name="sid" value="1001"></property><property name="sname" value="张三"></property><property name="age" value="20"></property><property name="gender" value=""></property><property name="clazz"><bean id="clazz_inner" class="poij.clazz"><property name="cid" value="333"></property><property name="name" value="卷王班"></property></bean></property><!--  为类型为数组的hobby属性赋值,数组的值为字面量,则使用value标签,如果为对象类型,则使用ref标签,通过的id从而获取对应的bean --><property name="hobby"><array><value>吃饭</value><value>睡觉</value><value>打豆豆</value></array></property></bean>

输出如下所示:

student{sid=1001, sname='张三', age=20, gender='男', clazz=clazz{cid=333, name='卷王班'}, hobby=[吃饭, 睡觉, 打豆豆]}

依赖注入之为集合类型的属性赋值:

修改clazz类:

//1个班级可以有多个学生,因此将学生作为属性添加到班级中是以集合的形式
private List<student> students;//为其编写set和get方法
public List<student> getStudents() {return students;}public void setStudents(List<student> students) {this.students = students;}//重写toString方法@Overridepublic String toString() {return "clazz{" +"cid=" + cid +", name='" + name + '\\'' +", students=" + students +'}';
}

内部list集合为集合类型的属性赋值:

修改spring.xml文件:

<!-- student1学生-->
<bean id="student1" class="poij.student"><property name="sid" value="1"></property><property name="sname" value="张三"></property><property name="age" value="20"></property><property name="gender" value=""></property><property name="hobby"><array><value>吃饭</value><value>睡觉</value></array></property></bean><!-- student2学生-->
<bean id="student2" class="poij.student"><property name="sid" value="2"></property><property name="sname" value="李四"></property><property name="age" value="12"></property><property name="gender" value=""></property><property name="hobby"><array><value>唱歌</value><value>打游戏</value></array></property></bean><!-- student3学生-->
<bean id="student3" class="poij.student"><property name="sid" value="3"></property><property name="sname" value="lisa"></property><property name="age" value="21"></property><property name="gender" value=""></property><property name="hobby"><array><value>弹钢琴</value><value>跳舞</value></array></property></bean><!-- clazz对象--在内部使用list标签,将上述的3个学生添加到班级当中-->
<bean id="clazz1" class="poij.clazz"><property name="cid" value="111"></property><property name="name" value="软件1班"></property><property name="students"><list><ref bean="student1"></ref><ref bean="student2"></ref><ref bean="student3"></ref></list></property>
</bean>

输出如下所示:

clazz{cid=111, name='软件1班', students=[student{sid=1, sname='张三', age=20, gender='女', clazz=null, hobby=[吃饭, 睡觉]}, student{sid=2, sname='李四', age=12, gender='男', clazz=null, hobby=[唱歌, 打游戏]}, student{sid=3, sname='lisa', age=21, gender='女', clazz=null, hobby=[弹钢琴, 跳舞]}]}

引用list集合的bean为集合类型的属性赋值:

如果在IOC容器中存在一个list集合类型的bean,那么我们是否可以直接通过ref来引用呢?如下所示:

依赖注入~

上述这种方式是不对的, 我们现在要创建的是一个list集合类型的bean最主要的是要往list集合中去存储数据但如果我们单独创建一个bean,类型是ArrayList,我们能做的只是为当前这个类中的属性赋值,而不能通过bean标签为当前的这个集合去存取数据

正确方法:

我们去配置一个集合类型的bean,但这需要使用util约束

添加前spring.xml文件中是没有该约束的:

依赖注入~

添加之后新增util约束:

依赖注入~

修改spring.xml文件中的内容:

<!-- student1 -->
<bean id="student1" class="poij.student"><property name="sid" value="1"></property><property name="sname" value="张三"></property><property name="age" value="20"></property><property name="gender" value=""></property><property name="hobby"><array><value>吃饭</value><value>睡觉</value></array></property></bean><!-- student2 --><bean id="student2" class="poij.student"><property name="sid" value="2"></property><property name="sname" value="李四"></property><property name="age" value="12"></property><property name="gender" value=""></property><property name="hobby"><array><value>唱歌</value><value>打游戏</value></array></property></bean><!-- student3 --><bean id="student3" class="poij.student"><property name="sid" value="3"></property><property name="sname" value="lisa"></property><property name="age" value="21"></property><property name="gender" value=""></property><property name="hobby"><array><value>弹钢琴</value><value>跳舞</value></array></property></bean><!-- clazz对象-->
<bean id="clazz1" class="poij.clazz"><property name="cid" value="111"></property><property name="name" value="软件1班"></property><!-- 引用list集合的bean--><property name="students" ref ="studentsList"></property>
</bean><!-- 配置一个集合类型的bean,需要使用util约束-->
<util:list id="studentsList"><ref bean="student1"></ref><ref bean="student2"></ref><ref bean="student3"></ref>
</util:list>

输出如下所示:

clazz{cid=111, name='软件1班', students=[student{sid=1, sname='张三', age=20, gender='女', clazz=null, hobby=[吃饭, 睡觉]}, student{sid=2, sname='李四', age=12, gender='男', clazz=null, hobby=[唱歌, 打游戏]}, student{sid=3, sname='lisa', age=21, gender='女', clazz=null, hobby=[弹钢琴, 跳舞]}]}

依赖注入之为map集合类型的属性赋值:

创建新的teacher类:

package poij;public class teacher {private  Integer tid;private  String name;public Integer getTid() {return tid;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void setTid(Integer tid) {this.tid = tid;}public teacher() {}public teacher(Integer tid,String name) {this.tid = tid;this.name=name;}@Overridepublic String toString() {return "teacher{" +"tid=" + tid +", name='" + name + '\\'' +'}';}
}

修改student:

//将老师作为学生的一个属性添加进来
private Map<String,teacher> stringteacherMap;//为其设置set和get方法public Map<String, teacher> getStringteacherMap() {return stringteacherMap;}public void setStringteacherMap(Map<String, teacher> stringteacherMap) {this.stringteacherMap = stringteacherMap;}//重写toString方法
@Overridepublic String toString() {return "student{" +"sid=" + sid +", sname='" + sname + '\\'' +", age=" + age +", gender='" + gender + '\\'' +", clazz=" + clazz +", hobby=" + Arrays.toString(hobby) +", stringteacherMap=" + stringteacherMap +'}';}

方法1:通过map标签进行设置

修改spring.xml文件:

假设我们现在要给id为student3的bean对象的stringteachermap属性赋值,如下所示,我们需要使用map标签而不是util:map,因为util:map的功能类似于util:list,它是用来配置一个类型为map的bean对象

依赖注入~

由于map中的数据是以键值对的形式存取的,那么如何设置键值对呢?

map标签中我们输入<,显示的结果有以下两种,看到entry不知道大家是否会感到格外的熟悉,因为这是我们在java中就已经接触过的,它表示的是一个类型,map中的键和值,我们可以使用一个entry来表示map集合中的一个键值对

依赖注入~

entry中可包含的参数有以下几种:和我们上面学习的其他类型是一样的,凡是带有-ref的则证明这个参数的类型是一个对象,若仅仅是key和value,则证明这个参数是字面量

依赖注入~

修改spring.xml文件中的内容:

<!-- 为id为student3的bean对象的stringteachermap属性赋值-->
<bean id="student3" class="poij.student"><property name="sid" value="3"></property><property name="sname" value="lisa"></property><property name="age" value="21"></property><property name="gender" value=""></property><property name="hobby"><array><value>弹钢琴</value><value>跳舞</value></array></property><!-- 在student中,map<String,teacher>--><property name="stringteacherMap"><map ><!-- teacher则使用引用外部bean的方式--><entry key="1号老师" value-ref="teacher1"></entry><entry key="2号老师" value-ref="teacher2"></entry></map></property></bean>
<!-- 创建类型为老师的bean对象-->
<!-- teacher1-->
<bean id="teacher1" class="poij.teacher"><property name="tid" value="10086"></property><property name="name" value="大宝"></property>
</bean><!-- teacher2-->
<bean id="teacher2" class="poij.teacher"><property name="tid" value="10010"></property><property name="name" value="小宝"></property>
</bean>

方法2:通过util:map进行设置

对spring.xml进行修改:

<!-- 创建了类型为老师的bean对象-->
<bean id="teacher1" class="poij.teacher"><property name="tid" value="10086"></property><property name="name" value="大宝"></property>
</bean><bean id="teacher2" class="poij.teacher"><property name="tid" value="10010"></property><property name="name" value="小宝"></property>
</bean>
<!-- 为id为student3的bean对象的stringteachermap属性赋值-->
<bean id="student3" class="poij.student"><property name="sid" value="3"></property><property name="sname" value="lisa"></property><property name="age" value="21"></property><property name="gender" value=""></property><property name="hobby"><array><value>弹钢琴</value><value>跳舞</value></array></property><!-- 直接通过id值引用util:map对象--><property name="stringteacherMap" ref="teacherMap"></property>
</bean><!-- 创建util:map对象-->
<util:map id="teacherMap"><entry key="1号老师" value-ref="teacher1"></entry><entry key="2号老师" value-ref="teacher2"></entry>
</util:map>

无论上述那种方法,输出均为如下所示:

student{sid=3, sname='lisa', age=21, gender='女', clazz=null, hobby=[弹钢琴, 跳舞], stringteacherMap={1号老师=teacher{tid=10086, name='大宝'}, 2号老师=teacher{tid=10010, name='小宝'}}}

依赖注入之为p命名空间:

在spring.xml文件中创建新的bean对象:

<bean id="student4" class="poij.student" p:sid="12" p:sname="小白" p:stringteacherMap-ref="teacherMap"></bean>

但我们使用上述这种p属性开头的方法,需要注意一定要引入p。它必须有约束的支持才能够进行,如下所示:

依赖注入~

使用p:开头的这种方式,我们会发现每个属性都有两个:一个是不带-ref后缀,一个是带-ref后缀,在前面的学习中,我们就说过带-ref表示该属性的值是一个类,而不带-ref的表示该属性的值是一个字面量,注意在选择的时候,不要选择错误了

依赖注入~

在测试类中获取id为student4的bean对象:

student student=ioc.getBean("student4", poij.student.class);
System.out.println(student);

输出如下所示,我们未赋值的属性是以null显示:

student{sid=12, sname='小白', age=null, gender='null', clazz=null, hobby=null, stringteacherMap={1号老师=teacher{tid=10086, name='大宝'}, 2号老师=teacher{tid=10010, name='小宝'}}}

这种方式虽然可行,但并不是常用的,因此我们了解一下即可!