Spring, Inversion of Control and Dependency Injection

Sudhanshu Paliwal
5 min readMar 15, 2021

In this Blog we are going to learn what is Spring and Inversion of Control and Dependency Injection

Spring

The Spring Web model-view-controller (MVC) structure is planned around a DispatcherServlet that dispatches requests to controllers, with configurable overseer mappings, see goal, area and subject goal just as help for transferring records. The default overseer depends on the @Controller and @RequestMapping annotations, offering a wide scope of adaptable taking care of strategies. With the presentation of Spring 3.0, the @Controller component likewise permits you to make RESTful Web locales and applications, through the @PathVariable annotation and different highlights.

MVC Framework

The Spring Framework includes its own model–see regulator (MVC) web application structure, which wasn’t initially arranged. The Spring engineers chose to compose their own Web structSuppose class One needs the object of class Two to instantiate or operate a method, then class One is said to be dependent on class Two. Now though it might appear okay to depend a module on the other but, in the real world, this could lead to a lot of problems, including system failure. Hence such dependencies need to be avoided.ure as a response to what they saw as the helpless plan of the (at that point) famous Jakarta Struts Web system just as insufficiencies in other accessible structures. Specifically, they felt there was lacking partition between the introduction and solicitation dealing with layers, and between the solicitation taking care of layer and the model.

WorkFlow

At the point when a client clicks a connection or presents a structure in their internet browser, the solicitation goes to Spring DispatcherServlet. DispatcherServlet is a front-regulator in spring MVC. It counsels at least one overseer mappings. DispatcherServlet has been picked as a proper regulator and advances the solicitation to it. The Controller measures the specific solicitation and creates an outcome. It is known as Model. This data should be arranged in html or any front-end innovation like JSP. This is the View of an application. The entirety of the data is in the MODEL And VIEW object. At the point when the regulator isn’t coupled to a specific view, DispatcherServlet tracks down the genuine JSP with the assistance of ViewResolver.

Inversion Of Control

In computer programming, inversion of control (IoC) is a programming rule. IoC alters the progression of control when contrasted with traditional control stream. In IoC, exclusively composed bits of a PC program get the progression of control from a conventional system. A Software architecture with this design transforms control when contrasted with conventional procedural programming: in customary programming, the custom code that communicates the motivation behind the program calls into reusable libraries to deal with nonexclusive assignments, however with reversal of control, the system calls into the custom, or errand explicit, code.

Let’s try to understand this by an example

Approach 1

public class ComplexAlgorithmImpl {
BubbleSortAlgorithm bubbleSortAlgorithm = new BubbleSortAlgorithm();
//…
}

Approach 2

public interface SortAlgorithm {
public int[] sort(int[] numbers);
}

@Component
public class ComplexAlgorithmImpl {
@AutoWired
private SortAlgorithm sortAlgorithm;
//…
}

Approach 1 is tied to a specific type of algorithm but on the other hand in approach 2 we can work with any type of algorithm. So in the inversion of control instead of creating it’s own dependencies, class declares it’s dependencies and then relies on the user to provide it.

Dependency Injection

Dependency Injection is the primary functionality given by Spring IOC(Inversion of Control). The Spring-Core module is liable for injecting dependencies through one or the other Constructor or Setter methods. The plan standard of Inversion of Control underscores keeping the Java classes autonomous of one another and the compartment liberates them from object creation and upkeep. These classes, overseen by Spring, should stick to the standard meaning of Java-Bean. Reliance Injection in Spring likewise guarantees free coupling between the classes

Need For Dependency Injection

Suppose class One needs the object of class Two to instantiate or operate a method, then class One is said to be dependent on class Two. Now though it might appear okay to depend a module on the other but, in the real world, this could lead to a lot of problems, including system failure. Hence such dependencies need to be avoided.

Spring IOC resolves such conditions with Dependency Injection, which makes the code simpler to test and reuse. Loose coupling between classes can be conceivable by characterizing interfaces for basic usefulness and the injector will start up the objects of required execution. The errand of starting up objects is finished by the compartment as per the setups indicated by the engineer.

Types Of Dependency Injection

1.Constructor Dependency Injection

In this, the DI will be injected with the help of contructors. Now to set the DI as CDI in bean, it is done through the bean-configuration file For this, the property to be set with the CDI is declared under the <constructor-arg> tag in the bean-config file.

package com.demo.org;
import com.demo.org.Check;
public class ABC {
// The object of the interface ABC
ABC abc;// Constructor to set the CDIbeansxmlns="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-2.5.xsd">
<bean id="GFG" class="com.geeksforgeeks.org.GFG">
<constructor-arg>
<bean class="com.geeksforgeeks.org.impl.CsvGFG" />
</constructor-arg>
</bean>
<bean id="CsvGFG" class="com.geeksforgeeks.org.impl.CsvGFG" /><bean id="JsonGFG" class="com.geeksforgeeks.org.impl.JsonGFG" /></beans>ABC(Check abc)
{
this.abc = abc;
}
}

Config Settings

<beansxmlns="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-2.5.xsd">
<bean id="GFG" class="com.geeksforgeeks.org.GFG">
<constructor-arg>
<bean class="com.geeksforgeeks.org.impl.CsvGFG" />
</constructor-arg>
</bean>
<bean id="CsvGFG" class="com.geeksforgeeks.org.impl.CsvGFG" /><bean id="JsonGFG" class="com.geeksforgeeks.org.impl.JsonGFG" /></beans>

2. Setter Dependency Injection

This is the more straightforward of the two DI techniques. In this, the DI will be infused with the assistance of setter or potentially getter strategies. Presently to set the DI as SDI in the bean, it is done through the bean-setup record For this, the property to be set with the SDI is proclaimed under the <property> tag in the bean-config document.

package com.demo.org;
import com.demo.org.Check;
public class ABC {
// The object of the interface ABC
ABC abc;// Constructor to set the CDIpublic void setABC(Check abc)
{
this.abc = abc;
}
}

Config Settings

<beansxmlns="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-2.5.xsd">
<bean id="GFG" class="com.geeksforgeeks.org.GFG">
<property name="geek">
<ref bean="CsvGFG" />
</property>
</bean>
<bean id="CsvGFG" class="com.geeksforgeeks.org.impl.CsvGFG" /><bean id="JsonGFG" class="com.geeksforgeeks.org.impl.JsonGFG" /></beans>

--

--