How dependency injection works in Spring?

Dependency Injection

მიმოხილვა

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. 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, ერთი არის 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) {

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 კლასში გამოიყენება როგორც მშენებელი არგუმენტი მითითება Country კლასის ლობიო განმარტება. Now, ლობიო განისაზღვრება და სახაზო ერთად.

Listing 3: XML კონფიგურაციის ფაილი

<?xml version=”1.0″ encoding=”UTF-8″?>

<ლობიო 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”>

<!– განსაზღვრება ქვეყანა ლობიო –>

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

<მშენებელი-არგ ref =”stateBean”/>

</bean>

<!– განმარტება სახელმწიფო ლობიო –>

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

</bean>

</ლობიო>

შემდეგ არის მთავარი კლასის შესამოწმებლად მშენებელი დაფუძნებული ლობიო ინექცია.

Listing 4: მთავარი განაცხადის ფაილი

package com.techalpine.spring;

იმპორტი org.springframework.context.ApplicationContext;

იმპორტი org.springframework.context.support.FileSystemXmlApplicationContext;

საჯარო კლასის TestBeanInjection {

საჯარო სტატიკური ბათილად მთავარი(სიმებიანი[] args) {

ApplicationContext კონტექსტში = new FileSystemXmlApplicationContext(

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

ქვეყანა countryobj = (Country)context.getBean(“countryBean”);

countryobj.stateName();

}

}








Now, აწარმოებს კლასის როგორც ცალკე პროგრამა, და ეს გამოჩნდება შემდეგ გამომავალი კონსოლი.

Inside State constructor
Inside Country constructor
Inside State name: 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.

 

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