总结: 十分钟快速理解 Spring DI 依赖注入

待我称王封你为后i 2022-05-28 01:53 266阅读 0赞

依赖注入(Dependency Injection,DI)

  1. 所谓的依赖注入,是指程序在运行过程中,如果需要调用另一个对象协助时,无须在代码中创建被调用者,而是依赖于外部的注入.Spring的依赖注入对调用者和被调用者几乎没有任何要求,完全支持对POJO之间依赖关系的管理.依赖注入通常有以下两种(以一个人需要一把斧子为例).
  2. 1.设值注入
  3. 设值注入是指通过setter方法传入被调用者的实例,这种注入方式简单,直观,因而在Spring的依赖注入里大量使用.看下面的代码,Person接口代码如下:
  4. //定义的Person接口
  5. public interface Person{
  6. //Person接口里定义一个使用斧子的方法
  7. public void useAxe();
  8. }
  9. //定义Axe接口
  10. public interface Axe{
  11. //接口里有一个砍的方法
  12. public void chop();
  13. }

Person实现类代码如下:

  1. //chinese 实现Person接口
  2. public class Chinese implements Person{
  3. //面向Axe接口编程,而不是具体的实现类
  4. private class axe;
  5. //默认的构造器
  6. public Chinese(){}
  7. //设置注入所需的setter方法
  8. public void setAxe(Axe axe){
  9. this.axe = axe;
  10. }
  11. //实现Person接口的useAxe方法
  12. public void useAxe(){
  13. System.out.println(axe.chop());
  14. }
  15. }

下面采用Spring的配置文件将Person实例和Axe实例组织在一起,配置文件代码如下:

  1. <!-- 下面是标准的XMl文件头 -->
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <!-- 下面一行定义的是Spring的xml配置文件的dtd -->
  4. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns="http://www.springframework.org/schema/beans"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:aop="http://www.springframework.org/schema/aop"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  11. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  12. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
  13. <BEANS>
  14. <!-- 定义第1bean ,该bean的id是Chinese,class指定该bean实例的实现类 -->
  15. <BEAN id = "chinese" class="com.Chinese">
  16. <!-- property元素用来指定需要容器注入的属性,axe属性需要容注入,此处是设值注入,因此chinese类必须拥有setAxe方法 -->
  17. <property name="axe" ref="stoneAxe"></property>
  18. </BEAN>
  19. <!--定义 stoneAxe bean -->
  20. <BEAN id = "stoneAxe" class="com.StoneAxe" />
  21. </BEANS>
  22. 从配置文件中,可以看到Spring管理Bean的灵巧性.Bean与Bean之间的依赖关系放在配置文件里组织,而不是写在代码里.通过配置文件的指定,Spring能精确地为每个Bean注入属性.因此,配置文件里Bean的class元素,不能仅仅是接口,而必须是真正的实现类.
  23. 2.构造注入
  24. 所谓构造注入,指通过构造函数来完成依赖关系的设定,而不是通过Setter方法.对前面代码Chinese类做简单修改,修改后的代码如下:
  25. //chinese 实现Person接口
  26. public class Chinese implements Person{
  27. //面向Axe接口编程,而不是具体的实现类
  28. private class axe;
  29. //默认的构造器
  30. public Chinese(){}
  31. //注入带参数的构造器
  32. public Chinese(Axe axe){
  33. this.axe = axe;
  34. }
  35. //实现Person接口的useAxe方法
  36. public void useAxe(){
  37. System.out.println(axe.chop());
  38. }
  39. }
  40. 此时无需Chinese类里的setAxe方法,构造Person实例时,Spring为Person实例注入所依赖的Axe实例.构造注入的配置文件也需做简单的修改,修改的配置文件如下:
  41. <!-- 下面是标准的XMl文件头 -->
  42. <?xml version="1.0" encoding="utf-8"?>
  43. <!-- 下面一行定义的是Spring的xml配置文件的dtd -->
  44. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  45. xmlns="http://www.springframework.org/schema/beans"
  46. xmlns:context="http://www.springframework.org/schema/context"
  47. xmlns:aop="http://www.springframework.org/schema/aop"
  48. xmlns:tx="http://www.springframework.org/schema/tx"
  49. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
  50. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
  51. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
  52. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
  53. <BEANS>
  54. <!-- 定义第1bean ,该bean的id是Chinese,class指定该bean实例的实现类 -->
  55. <BEAN id = "chinese" class="com.Chinese">
  56. </BEAN>
  57. <!--定义 stoneAxe bean -->
  58. <BEAN id = "stoneAxe" class="com.StoneAxe" />
  59. </BEANS>
  60. 执行效果与使用setAxe设值注入时的执行效果完全一样.区别在于:创建Person实例中Axe属性的时机不同----设值注入是先创建一个默认的bean实例,然后调用对应的构造方法注入依赖关系.而构造注入则在创建bean实例时,已经完成了依赖关系.
  61. 其实,所谓依赖注入,是指程序在运行过程中,如果需要调用另一个对象协助,无需在代码中创建被调用者,而是依赖于外部的注入.
  62. Spring中可以通过设值注入和构造注入两种方式来实现

发表评论

表情:
评论列表 (有 0 条评论,266人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SpringDI(依赖注入)

    依赖注入(DI)是一个过程,在这个过程中,对象仅通过构造函数参数、工厂方法的参数或在对象被实例化后通过属性设置来定义它们的依赖项(即与该对象一起工作的其他对象)。然后,容器在创

    相关 Spring---DI依赖注入

    上一篇介绍了关于IOC的内容:IOC称为控制反转,简单来说就是讲对象的创建的权利以及对象的生命周期的管理过程交给Spring容器来管理,从此在开发的过程中不需要关注对象的创建以

    相关 Spring依赖注入DI

    开头语:本文是我整理网上各种资料后整合而成的一篇文章。文中涉及到了代码重构、面向接口编程、持久化和工厂设计模式的内容。 1. Spring是什么?