How dependency injection works in Spring?

Dependency Injection

Overview

Spring bean injection is also popularly known as ‘Dependency Injection (OF)’. 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. Յուրաքանչյուր փաստարկն կառուցապատողի ը կախվածության օբյեկտ / լոբի. The Spring բեռնարկղ վկայակոչում է շինարարներին հետ փաստարկներով.

Եկեք ստուգել մեկ օրինակը. Կան երկու դասեր, մեկն է երկիր դասի, իսկ մյուսը ` պետական դաս. The երկիր դասի ունի կախվածությունը պետական դաս. So, մենք պետք է անցնեն կախվածությունը դեպի երկիր դասի իր շինարար (as shown below).

Listing 1: Երկիր դասը

փաթեթը com.techalpine.spring;

հասարակական կարգի Երկիր {

մասնավոր State պետություն;

հասարակական Երկիր(պետական ​​պետություն) {

System.out.println(“Inside Country կոնստրուկտոր:” );

this.state = պետություն;

}

հասարակական առոչինչ stateName() {

state.stateName();

}

}








Now, Սա կախվածությունը կարող է օգտագործվել զանգահարել տարբեր մեթոդներ.

Ստորեւ ներկայացվում է պետական դասի կոդ հատվածներ.

Listing 2: պետական ​​կարգի

փաթեթը com.techalpine.spring;

հասարակական կարգի պետական {

հասարակական պետական(){

System.out.println(“Inside պետական ​​կոնստրուկտոր:” );

}

հասարակական առոչինչ stateName() {

System.out.println(“Inside պետական ​​անունով: West Bengal”);

}

}

Ստորեւ ներկայացվում է XML կազմաձեւման ֆայլը մետաղալարեր այդ լոբու եւ նրանց կախվածությունը. Լոբին ID ('StateBean') of the պետական class is used as a constructor argument reference in the երկիր 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

փաթեթը com.techalpine.spring;

ներմուծման org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class TestBeanInjection {

հասարակական ստատիկ անվավեր հիմնական(Լար[] args) {

ApplicationContext context = new FileSystemXmlApplicationContext(

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

Country countryobj = (երկիր)context.getBean(“countryBean”);

countryobj.stateName();

}

}








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

Inside State constructor
Inside Country constructor
Inside պետական ​​անունով: West Bengal

Setter Bases DI

In a Setter based DI, dependencies are passed through setter methods. Spring container invokes those setter methods to instantiate the beans. Here, the mechanism and the components are almost similar to the constructor based DI. The only difference is, in the passing of dependencies and the XML configuration. In a setter based injection, <սեփականություն> tags are used instead of <constructor-arg> tag.








Ամփոփում

In this article we have explained the ways how spring beans can be injected. There is no clear-cut rule when to use setter based or constructor based DI. We generally go for constructor based DI when the dependencies are mandatory, and select setter-based DI when the dependencies are optional. But combinations of both are also allowed.

 

============================================= ============================================== Գնեք լավագույն տեխնոլոգիական գրքերը Amazon- ում,en,Էլեկտրականություն CT շագանակագույն էլեկտրաէներգիա,en
============================================== ---------------------------------------------------------------- electrician ct chestnutelectric
error

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share