在 Spring 框架中,依赖注入机制(DI)多用于对象间的依赖,也称之为IOC(控制反转)
大致意思是:程序在运行的时候,由外部容器动态的将依赖对象注入到组件中,比如当Spring 容器启动时,spring开始进行容器初始化,创建并管理bean对象,以及销毁他。所以我们不需要自己编写代码来创建bean对象,可以直接从spring容器中获取,此时控制权就发生转移,由应用程序转移到外部容器,即控制反转。

主要实现方式有两种:

  • setter 方法注入
  • 构造函数注入

setter 方法注入

  • 该方法目前在Spring中最为常用
  • 例子
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// OutputHelper.java
package com.walterlife.blog;

import com.walterlife.blog.IOutputGenerator;

public class OutputHelper {
IOutputGenerator outputGenerator;

public void generateOutput() {
outputGenerator.generateOutput();
}

public void setOutputGenerator(IOutputGenerator outputGenerator) {
this.outputGenerator = outputGenerator;
}
}

// IOutputGenerator.java
package com.walterlife.blog;

public interface IOutputGenerator {
public void generateOutput();
}

// CsvOutputGenerator.java
package com.walterlife.blog;

import com.walterlife.blog.IOutputGenerator;

public class CsvOutputGenerator implements IOutputGenerator {
public void generateOutput() {
System.out.println("Csv Output Generator");
}
}

// JsonOutputGenerator.java
package com.walterlife.blog;

import com.walterlife.blog.IOutputGenerator;

public class JsonOutputGenerator implements IOutputGenerator {
public void generateOutput() {
System.out.println("Json Output Generator");
}
}
  • 在 资源 xml文件中配配beans, 用来设置对象间的依赖关系

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    <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-2.5.xsd">


    <bean id="OutputHelper" class="com.walterlife.blog.OutputHelper">
    <!-- 通过 构造函数注入 -->
    <property name="outputGenerator">
    <ref bean="CsvOutputGenerator" />
    </property>
    </bean>

    <bean id="CsvOutputGenerator" class="com.walterlife.blog.CsvOutputGenerator" />
    <bean id="JsonOutputGenerator" class="com.walterlife.blog.JsonOutputGenerator" />

    </beans>
  • 通过如上配置,你就通过一个 setter(setOutputGenerator) 方法 将 CsvOutputGenerator bean 注入到 OutputHelper对象啦

构造函数注入

  • 修改OutputHelper.java,增加构造函数

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    // OutputHelper.java
    package com.walterlife.blog;

    import com.walterlife.blog.IOutputGenerator;

    public class OutputHelper {
    IOutputGenerator outputGenerator;

    public void generateOutput() {
    outputGenerator.generateOutput();
    }

    OutputHelper(IOutputGenerator outputGenerator) {
    this.outputGenerator = outputGenerator;
    }
    }
  • 在 资源 xml文件中配配beans, 用来设置对象间的依赖关系

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    <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-2.5.xsd">


    <bean id="OutputHelper" class="com.walterlife.blog.OutputHelper">
    <!-- 通过 setter 注入 -->
    <property name="outputGenerator" ref="JsonOutputGenerator" />
    <property name="outputGenerator" ref="CsvOutputGenerator" />
    </bean>

    <bean id="CsvOutputGenerator" class="com.walterlife.blog.CsvOutputGenerator" />
    <bean id="JsonOutputGenerator" class="com.walterlife.blog.JsonOutputGenerator" />

    </beans>
  • 通过如上配置,你就通过构造函数(OutputHelper) 将 CsvOutputGenerator bean 注入到 OutputHelper对象啦

该选择哪种注入方式

  • 两种方式没有好坏之分,看自己怎么选择,但是setter 方式比较简单所以选择这种方式的人比较多

留言

Jul 16 2015