如果在 Class A 中,有 Class B 的实例,则称 Class A 对 Class B 有一个依赖。例如下面类 Human 中用到一个 Father 对象,我们就说类 Human 对类 Father 有一个依赖。
public class Human { Father father;}
一般的创建依赖的方式以及带来的问题
看一段代码:
public class Human { Father father; public Human() { father = new Father(); }}
从上述代码可以很明显地看出,调用者(Human)主动创建被依赖对象(Father),然后再调用被依赖对象的方法(此处未体现),对于这种方式,由于调用者需要通过形如“new 被依赖对象构造器();” 的代码创建对象,因此必然导致调用者与被依赖对象实现类的硬编码耦合,非常不利于项目升级的维护。带有的问题可能有:
1)如果现在要改变 father 生成方式,如需要用new Father(String name)初始化 father,需要修改 Human 代码;
2)如果想测试不同 Father 对象对 Human 的影响很困难,因为 father 的初始化被写死在了 Human 的构造函数中;
3)如果new Father()过程非常缓慢,单元测试时我们希望用已经初始化好的 father 对象 Mock 掉这个过程也很困难。
(推荐一篇博客,我个人的理解不深,下面的这段话与具体的例子都源自)
每个基于应用程序的 java 都有几个对象,这些对象一起工作来呈现出终端用户所看到的工作的应用程序。当编写一个复杂的 Java 应用程序时,应用程序类应该尽可能独立于其他 Java 类来增加这些类重用的可能性,并且在做单元测试时,测试独立于其他类的独立性。依赖注入(或有时称为布线)有助于把这些类粘合在一起,同时保持他们独立。
Spring 基于构造函数的依赖注入(Dependency Injection简称DI):
当容器调用带有一组参数的类构造函数时,基于构造函数的 DI 就完成了,其中每个参数代表一个对其他类的依赖。
接下来,我们将通过示例来理解 Spring 基于构造函数的依赖注入。
SpellChecker.java
package com.how2java.w3cschool.di;public class SpellChecker { public SpellChecker() { System.out.println("在SpellChecker的构造方法中!"); } public void checkSpelling() { System.out.println("在checkSpelling方法中"); }}
TextEditor.java
package com.how2java.w3cschool.di; public class TextEditor { private SpellChecker spellChecker; public TextEditor(SpellChecker spellChecker) { //该类的构造方法的参数是其他的类 System.out.println("在TextEditor的构造方法中!"); this.spellChecker = spellChecker; } public void spellCheck() { spellChecker.checkSpelling(); } }
MainApp.java
package com.how2java.w3cschool.di;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beandi.xml"); TextEditor te = (TextEditor)context.getBean("textEditor"); te.spellCheck(); }}
配置文件:beandi.xml
结果如下:
构造函数参数解析:
如果存在不止一个参数时,当把参数传递给构造函数时,可能会存在歧义。要解决这个问题,那么构造函数的参数在 bean 定义中的顺序就是把这些参数提供给适当的构造函数的顺序就可以了。
基于设值函数的依赖注入(也称为设值注入)
设值注入是指IOC容器通过成员变量的setter方法来注入被依赖对象。这种注入方式简单、直观,因而在Spring的依赖注入里大量使用。
以下是一个基于设值函数的注入来实现依赖注入例子:
TextEditor.java
package com.tutorialspoint;public class TextEditor { private SpellChecker spellChecker; // a setter method to inject the dependency. public void setSpellChecker(SpellChecker spellChecker) { System.out.println("Inside setSpellChecker." ); this.spellChecker = spellChecker; } // a getter method to return spellChecker public SpellChecker getSpellChecker() { return spellChecker; } public void spellCheck() { spellChecker.checkSpelling(); }}
SpellChecker.java
package com.tutorialspoint;public class SpellChecker { public SpellChecker(){ System.out.println("Inside SpellChecker constructor." ); } public void checkSpelling() { System.out.println("Inside checkSpelling." ); } }
MainApp.java
package com.tutorialspoint;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); TextEditor te = (TextEditor) context.getBean("textEditor"); te.spellCheck(); }}
Beans.xml
1 2 37 8 9 10 12 13 1411 15 16 17
Spring会自动检测每个 <bean.../>定义里的<property.../>元素定义,Spring会在调用默认的构造器创建Bean实例之后,立即调用对应的setter方法为Bean的成员变量注入值(在本例中,请看Beans.xml的第10行中的property name="spellChecker" 可知会调用 setSpellChecker方法为id为textEditor的Bean的成员变量注入值)