How dependency injection works in Spring?

Dependency Injection

Apèsi sou lekòl la

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 klas. The Country class has a dependency on the State klas. So, we will pass the dependency to the Country class through its constructor (jan sa endike anba).

Listing 1: Country class

package com.techalpine.spring;

public class Country {

private State state;

public Country(State state) {

System.out.println(“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(){

System.out.println(“Inside State constructor.” );

}

public void stateName() {

System.out.println(“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”>

<!– Definisyon pou pwa Peyi –>

<pwa didantite =”countryBean” class =”com.techalpine.spring.Country”>

<konstrukteur-ARG ref =”stateBean”/>

</pwa>

<!– Definisyon pou pwa Eta –>

<pwa didantite =”stateBean” class =”com.techalpine.spring.State”>

</pwa>

</pwa>

Sa yo se klas la prensipal li teste konstrukteur ki baze sou piki a pwa.

Listing 4: Main dosye aplikasyon

package com.techalpine.spring;

import org.springframework.context.ApplicationContext;

enpòte org.springframework.context.support.FileSystemXmlApplicationContext;

klas piblik TestBeanInjection {

piblik anile estatik prensipal(Fisèl[] arg) {

ApplicationContext kontèks = nouvo FileSystemXmlApplicationContext(

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

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

countryobj.stateName();

}

}








Now, kouri klas la kòm yon aplikasyon-kanpe pou kont epi li pral montre pwodiksyon an yo sou konsole a.

Anndan konstrukteur Eta
Anndan Peyi konstrukteur
Inside State name: West Bengal

Ansanbl Baz DI

Nan yon DI meteur ki baze, Dependencies yo te pase nan mitan metòd meteur. veso Spring susit moun metòd meteur enstansye pwa yo. Here, mekanis a ak konpozan yo se prèske menm jan ak DI la konstrukteur ki baze. Sèl diferans ki genyen se, nan pase a nan Dependencies ak configuration la, te XML. Nan yon piki meteur ki baze, <pwopriyete> Tags yo te itilize olye pou yo <konstrukteur-ARG> tag.








Konklizyon

Nan atik sa a nou te eksplike fason yo ki jan pwa sezon prentan ka sou fòm piki. Pa gen okenn règ klè-koupe lè yo sèvi ak meteur baze sou oswa konstrukteur ki baze DI. Nou jeneralman ale pou konstrukteur ki baze DI lè Dependencies yo se obligatwa, epi chwazi meteur ki baze sou DI lè Dependencies yo se si ou vle. Men, konbinezon nan tou de yo tou pèmèt.

 

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

Enjoy this blog? Please spread the word :)

Follow by Email
LinkedIn
LinkedIn
Share