您的位置:首页 > 编程语言 > Java开发

Spring源码阅读 之依赖注入的实现方式

2016-06-13 13:27 573 查看
Spring是这样描述依赖注入的:

Dependency injection (DI) is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method,
or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control
(IoC), of the bean itself controlling the instantiation or location of its dependencies on its own by using direct construction of classes, or the Service Locator pattern.

Code is cleaner with the DI principle and decoupling is more effective when objects are provided with their dependencies. The object does not look up its dependencies, and does not know the location or class of the dependencies.
As such, your classes become easier to test, in particular when the dependencies are on interfaces or abstract base classes, which allow for stub or mock implementations to be used in unit tests.

DI exists in two major variants, Constructor-based dependency injection and Setter-based dependency injection.

给英语不好的同学解释下上面的这三段话的意思:

依赖注入是一个凭借对象定义它们的依赖的过程,依赖也就是需要一起协同工作的其他对象,当它被构造完成或者被工厂方法返回后,仅仅通过构造器参数、工厂方法参数或属性将依赖对象赋值给这个对象实例。容器注入这些依赖当它创建bean的时候。这个过程从本质上反转了bean自己控制实例化或(通过使用直接的构造器类或服务定位模式)定位它的依赖,因此得名控制反转(IoC)。

和对象自己提供他们的依赖的代码比起来,使用DI机制的代码可以更简洁并且有效的解耦。对象不需要查找他们的依赖也不需要知道依赖的位置和依赖的类型。因此你的类变得更容易测试,尤其当依赖是一个接口或者抽象基类的时候,这也允许你在测试用例中模拟依赖的实现。

DI有两个主要的变体:基于构造器的依赖注入和基于Setter的依赖注入。

注:控制反转和依赖注入描述的是同一事物,只是描述的角度不同,依赖注入是站在应用程序的角度来描述的,而控制反转是站在
容器的角度。描述的都是都是应用程序获取依赖的方式由原来的主动出击(想要什么就主动访问什么)变为被动接受,容器来给
的。

下面是能用到的几个代码片段:

Student.java
package com.dusk.bean;

import java.io.Serializable;

public class Student implements Cloneable,Serializable{
private String id;
private String name;
private String age;
private String sex;
private Address address;
public Student(){
}
public Student(String id){
this.id=id;
}
public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", sex=" + sex + "]";
}

public void sayHello(){
System.out.println("hello,everyone!");
}
@Override
public Student clone() throws CloneNotSupportedException {
Student stu=new Student();
stu.setId(this.id);
stu.setAddress(new Address());
return stu;
}

}
Hello.java
package com.dusk.bean;

public class Hello {
private String name;

public Hello(Hello hello){
this.name=hello.name+"_ref";
}
public Hello(Student stu){
this.name=stu.getName();
}
public Hello(){}

public void setName(String name) {
this.name = name;
}

public void sayHello(){
System.out.println("hello, "+name+"!");
}
}
Client.java
package com.dusk.bean;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class Client {
public static void main(String[] args) {
Resource resource=new ClassPathResource("com/dusk/bean/bean.xml");
BeanFactory beanFactory=new XmlBeanFactory(resource);
Hello hello=(Hello) beanFactory.getBean("hello");
hello.sayHello();
}
}



基于构造器的依赖注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="hello" class="com.dusk.bean.Hello">
<constructor-arg ref="student"></constructor-arg>
</bean>
<bean id="student" class="com.dusk.bean.Student">
<property name="name" value="dusk"/>
</bean>
</beans>


基于Setter的依赖注入

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="hello" class="com.dusk.bean.Hello">
<property name="name" value="dusk"/>
</bean>
</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: