How dependency injection works in Spring?

Dependency Injection

概观

Spring bean injection is also popularly known as ‘Dependency Injection (作者:)’. The heart of Spring framework is ‘Inversion Of Control (IoC)’ and DI is the implementation part of IoC. We will start our discussion with the assumption that the concept and purpose of IoC and DI is already know.

In this article, we will focus on different types of Spring bean injections with some practical coding examples.








Dependency Injection

Spring bean injections can be performed in two major ways, one is known as Constructor-based DI and the other one is known as Setter-based DI. In the following sub-sections we will check how these can be achieved.

Constructor Based DI

In a constructor based DI, dependencies are injected through constructors. 构造函数的每个参数是一个依赖对象/豆. Spring容器调用与参数的构造函数.

让我们看一个例子. 有两类, 一个是 国家 类,另一个是 类. “ 国家 类对依赖 类. So, 我们将通过依赖于 国家 通过其构造类 (as shown below).

Listing 1: 国家类

包com.techalpine.spring;

公共类国家 {

私有状态状态;

国家公(国家状态) {

System.out.println(“内部构造国家。” );

this.state =状态;

}

公共无效Statename的() {

state.stateName();

}

}








现在, 这种依赖性可以用来调用不同的方法.

以下是 类代码片段.

Listing 2: 州级

包com.techalpine.spring;

公共类国家 {

国家公(){

System.out.println(“国家内部构造。” );

}

公共无效Statename的() {

System.out.println(“内部状态名称: West Bengal”);

}

}

以下是XML配置文件接线那些豆类和它们的依赖关系. 在bean id (“stateBean”) of the class is used as a constructor argument reference in the 国家 class bean definition. 现在, the beans are defined and wired together.

Listing 3: XML configuration file

<?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-3.2.xsd”>

<!– Definition for Country bean –>

<bean id=”countryBean” class=”com.techalpine.spring.Country”>

<constructor-arg ref=”stateBean”/>

</bean>

<!– Definition for State bean –>

<bean id=”stateBean” class=”com.techalpine.spring.State”>

</bean>

</beans>

Following is the main class to test the constructor-based bean injection.

Listing 4: Main application file

包com.techalpine.spring;

进口org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestBeanInjection {

公共静态无效的主要(串[] 参数) {

ApplicationContext context = new FileSystemXmlApplicationContext(

“WebContent/WEB-INF/beanConfig.xml”);

Country countryobj = (国家)context.getBean(“countryBean”);

countryobj.stateName();

}

}








现在, run the class as a stand-alone application and it will show the following output on the console.

国家内部构造
内部构造国家
内部状态名称: West Bengal

集基地DI

在二传手基于DI, 依赖通过setter方法传递. Spring容器调用这些setter方法来实例化豆. Here, 机制和成分几乎类似于基于构造的DI. 唯一的区别是, 在依赖的传球和XML配置. 在二传手的注塑, <财产> 标签被用来代替 <构造精氨酸> tag.








结论

在这篇文章中,我们已经解释的Spring bean如何能够被注入方式. 没有什么时候使用基于setter方法或者构造基于DI明确的规则. 我们一般去构造基于DI时的依赖是强制性的, 并选择的Setter DI时的依赖是可选. 但两者的组合也是允许的.

 

============================================= ============================================== 在亚马逊上购买最佳技术书籍,en,电工CT Chestnutelectric,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share