How dependency injection works in Spring?

Dependency Injection

סקירה

Spring bean injection is also popularly known as ‘Dependency Injection (DI)’. 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. Each argument of the constructor is a dependency object/bean. The Spring container invokes the constructors with the arguments.

Let us check one example. There are two classes, one is the Country class and the other one is the State מעמד. The Country class has a dependency on the State מעמד. So, we will pass the dependency to the Country class through its constructor (as shown below).

Listing 1: Country class

package com.techalpine.spring;

public class Country {

private State state;

public Country(State state) {

שיטה(“Inside Country constructor.” );

this.state = state;

}

public void stateName() {

state.stateName();

}

}








Now, this dependency can be used to call different methods.

Following is the State class code snippets.

Listing 2: State class

package com.techalpine.spring;

public class State {

public State(){

שיטה(“Inside State constructor.” );

}

public void stateName() {

שיטה(“Inside State name: West Bengal”);

}

}

Following is the XML configuration file to wire those beans and their dependencies. The bean id (‘stateBean’) of the State class is used as a constructor argument reference in the Country class bean definition. Now, 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

package com.techalpine.spring;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestBeanInjection {

הריק סטטי הראשי ציבורי(חוט[] ארגומנטים) {

ApplicationContext context = new FileSystemXmlApplicationContext(

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

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

countryobj.stateName();

}

}








Now, להפעיל את הכיתה כיישום עצמאי והוא יציג את הפלט הבא על הקונסולה.

בתוך בנאי המדינה
בתוך בנאי המדינה
Inside State name: West Bengal

DI בסיסי סטים

בעוד DI מבוסס סתר, תלות מועברת באמצעות שיטות ציד. מיכל האביב מפעיל שיטות ציד אלה להפעלת השעועית. Here, המנגנון ואת הרכיבים הם כמעט דומים DI הבנאי מבוסס. ההבדל היחיד הוא, ב פטירתו של תלות בתצורת XML. הזרקה מבוססת סטר, <רכוש> תגים משמשים במקום <בנאים-arg> tag.








מסקנה

במאמר זה שבארנו הדרכים כיצד שעועית באביב ניתן להזריק. אין חוק ברור מתי להשתמש מבוסס סטר או בנאי מבוסס DI. בדרך כלל אנחנו הולכים על DI מבוסס בנאי כאשר התלות היא שדות חובה, and select setter-based DI when the dependencies are optional. But combinations of both are also allowed.

 

============================================= ============================================== Buy best TechAlpine Books on Amazon
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share