总结: 十分钟快速理解 Spring DI 依赖注入
依赖注入(Dependency Injection,DI)
所谓的依赖注入,是指程序在运行过程中,如果需要调用另一个对象协助时,无须在代码中创建被调用者,而是依赖于外部的注入.Spring的依赖注入对调用者和被调用者几乎没有任何要求,完全支持对POJO之间依赖关系的管理.依赖注入通常有以下两种(以一个人需要一把斧子为例).
1.设值注入
设值注入是指通过setter方法传入被调用者的实例,这种注入方式简单,直观,因而在Spring的依赖注入里大量使用.看下面的代码,Person接口代码如下:
//定义的Person接口
public interface Person{
//Person接口里定义一个使用斧子的方法
public void useAxe();
}
//定义Axe接口
public interface Axe{
//接口里有一个砍的方法
public void chop();
}
Person实现类代码如下:
//chinese 实现Person接口
public class Chinese implements Person{
//面向Axe接口编程,而不是具体的实现类
private class axe;
//默认的构造器
public Chinese(){}
//设置注入所需的setter方法
public void setAxe(Axe axe){
this.axe = axe;
}
//实现Person接口的useAxe方法
public void useAxe(){
System.out.println(axe.chop());
}
}
下面采用Spring的配置文件将Person实例和Axe实例组织在一起,配置文件代码如下:
<!-- 下面是标准的XMl文件头 -->
<?xml version="1.0" encoding="utf-8"?>
<!-- 下面一行定义的是Spring的xml配置文件的dtd -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<BEANS>
<!-- 定义第1bean ,该bean的id是Chinese,class指定该bean实例的实现类 -->
<BEAN id = "chinese" class="com.Chinese">
<!-- property元素用来指定需要容器注入的属性,axe属性需要容注入,此处是设值注入,因此chinese类必须拥有setAxe方法 -->
<property name="axe" ref="stoneAxe"></property>
</BEAN>
<!--定义 stoneAxe bean -->
<BEAN id = "stoneAxe" class="com.StoneAxe" />
</BEANS>
从配置文件中,可以看到Spring管理Bean的灵巧性.Bean与Bean之间的依赖关系放在配置文件里组织,而不是写在代码里.通过配置文件的指定,Spring能精确地为每个Bean注入属性.因此,配置文件里Bean的class元素,不能仅仅是接口,而必须是真正的实现类.
2.构造注入
所谓构造注入,指通过构造函数来完成依赖关系的设定,而不是通过Setter方法.对前面代码Chinese类做简单修改,修改后的代码如下:
//chinese 实现Person接口
public class Chinese implements Person{
//面向Axe接口编程,而不是具体的实现类
private class axe;
//默认的构造器
public Chinese(){}
//注入带参数的构造器
public Chinese(Axe axe){
this.axe = axe;
}
//实现Person接口的useAxe方法
public void useAxe(){
System.out.println(axe.chop());
}
}
此时无需Chinese类里的setAxe方法,构造Person实例时,Spring为Person实例注入所依赖的Axe实例.构造注入的配置文件也需做简单的修改,修改的配置文件如下:
<!-- 下面是标准的XMl文件头 -->
<?xml version="1.0" encoding="utf-8"?>
<!-- 下面一行定义的是Spring的xml配置文件的dtd -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
<BEANS>
<!-- 定义第1bean ,该bean的id是Chinese,class指定该bean实例的实现类 -->
<BEAN id = "chinese" class="com.Chinese">
</BEAN>
<!--定义 stoneAxe bean -->
<BEAN id = "stoneAxe" class="com.StoneAxe" />
</BEANS>
执行效果与使用setAxe设值注入时的执行效果完全一样.区别在于:创建Person实例中Axe属性的时机不同----设值注入是先创建一个默认的bean实例,然后调用对应的构造方法注入依赖关系.而构造注入则在创建bean实例时,已经完成了依赖关系.
其实,所谓依赖注入,是指程序在运行过程中,如果需要调用另一个对象协助,无需在代码中创建被调用者,而是依赖于外部的注入.
Spring中可以通过设值注入和构造注入两种方式来实现
还没有评论,来说两句吧...