Spring Dependency Injection (DI)

本文详细介绍了Spring框架中的依赖注入设计模式及其两种主要类型:设置注入和构造注入。通过示例展示了如何使用这两种方法来定义对象之间的依赖关系,并强调了选择适合项目需求的注入方式的重要性。

In Spring frameowork, Dependency Injection (DI) design pattern is used to define the object dependencies between each other. It exits in two major types :
- Setter Injection
- Constructor Injection

1. Setter Injection

This is the most popular and simple DI method, it will injects the dependency via a setter method.

Example

A helper class with a setter method.

package com.mkyong.output;

import com.mkyong.output.IOutputGenerator;

public class OutputHelper
{
    IOutputGenerator outputGenerator;

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

}

A bean configuration file to declare the beans and set the dependency via setter injection (property tag).

<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.mkyong.output.OutputHelper">
        <property name="outputGenerator">
            <ref bean="CsvOutputGenerator" />
        </property>
    </bean>

<bean id="CsvOutputGenerator" class="com.mkyong.output.impl.CsvOutputGenerator" />
<bean id="JsonOutputGenerator" class="com.mkyong.output.impl.JsonOutputGenerator" />

</beans>

You just injects a ‘CsvOutputGenerator’ bean into ‘OutputHelper’ object via a setter method (setOutputGenerator).

2. Constructor Injection

This DI method will injects the dependency via a constructor.

Example

A helper class with a constructor.

package com.mkyong.output;

import com.mkyong.output.IOutputGenerator;

public class OutputHelper
{
    IOutputGenerator outputGenerator;

        OutputHelper(IOutputGenerator outputGenerator){
        this.outputGenerator = outputGenerator;
    }
}

A bean configuration file to declare the beans and set the dependency via constructor injection (constructor-arg tag).

<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.mkyong.output.OutputHelper">
        <constructor-arg>
            <bean class="com.mkyong.output.impl.CsvOutputGenerator" />
        </constructor-arg>
    </bean>

<bean id="CsvOutputGenerator" class="com.mkyong.output.impl.CsvOutputGenerator" />
<bean id="JsonOutputGenerator" class="com.mkyong.output.impl.JsonOutputGenerator" />

</beans>

You just injects a ‘CsvOutputGenerator’ bean into ‘OutputHelper’ object via a constructor.

Setter or Constructor injection?

There are no hard rule set by Spring framework, just use whatever type of DI that suit your project needs. However, due to the simplicity of the setter injection, it’s always selected for most of the scenarios.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值