Geek Culture
Published in

Geek Culture

Inversion of Control and Dependency Injection

In this article, we will learn about inversion of control and dependency injection in spring

Spring Framework

Spring is a little framework with a lot of features. It’s sometimes referred to as a framework of frameworks because it supports a variety of frameworks, including Struts, Hibernate, Tapestry, EJB, JSF, and others. IOC, AOP, DAO, Context, ORM, WEB MVC, and other modules are part of the Spring framework.

Advantage of Spring framework

  • Easy to test: Testing the programme is made easier with Dependency Injection. Although an EJB or Struts application needs a server to run, the Spring framework does not.
  • Loose Coupling: It is easier to test the application with Dependency Injection. Spring framework does not require a server like EJB or Struts.
  • Predefined Template: The Spring framework includes templates for technologies such as JDBC, Hibernate, and JPA. As a result, there’s no need to write a lot of code. It hides the fundamental steps in these technologies.
  • Fast development: The Dependency Injection feature of Spring Framework and its support to various frameworks make the JavaEE application's easy development.

Inversion of Control

It’s a concept that involves handing up control of things to a container or framework. It’s most commonly used in relation to object-oriented programming. In traditional programming, our custom code calls a library; however, IoC allows a framework to take control of a program’s flow and call our code. The person in charge of object generation keeps track of the object in memory. At runtime, IOC builds the class’s object and makes it available wherever it is required. It is primarily responsible for ensuring that an object’s life cycle is maintained.

Bean: Beans are the objects in Spring that constitute the backbone of your application and are maintained by the Spring IoC container. A bean is an object that a Spring IoC container instantiates, assembles, and generally manages. A bean, on the other hand, is just one of many objects in your programme. Beans and their interdependencies are reflected in the container’s configuration metadata.

Spring Beans scope are divided into five categories of which two major are as follows:

Singleton: Each container will only have one instance of the bean. Spring beans have this as their default scope. If you use this scope, make sure that the bean doesn’t have any shared instance variables, as this could cause data inconsistency.

Prototype: A new instance will be created every time the bean is requested.

Dependency Injection

Dependency Injection is a pattern for implementing IoC. It enables the creation of dependant objects outside of a class and the providing of those objects to a class in a variety of ways. We use DI to move the creation and binding of dependent objects outside of the class on which they are dependent.

Types of dependency injections

  1. Constructor Injection
  2. Setter Injection
  3. Field Injection

Constructor Injection:

package com.app;import org.springframework.stereotype.Component;@Component
public class FortuneService{
public String getFortune() {
return "Today is your lucky day!";
}
}

Here is a class that has the getFortune() method and we want to use this method in another class.

package com.app;import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class Coach {
private FortuneService fortuneService;

@Autowired
public Coach(FortuneService fortuneService) {
this.fortuneService = fortuneService;
}
public String getDailyWorkout() {
return "Do cardio daily!!";
}
public String getDailyFortune() {
return fortuneService.getFortune();
}
}

and the configuration file is

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.app"/></beans>

Setter Injection:

package com.app;import org.springframework.stereotype.Component;@Component
public class FortuneService{
public String getFortune() {
return "Today is your lucky day!";
}
}

Here is a class that has the getFortune() method and we want to use this method in another class.

package com.app;import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class Coach {
private FortuneService fortuneService;

@Autowired
public void setFortuneService(FortuneService fortuneService) {
this.fortuneService = fortuneService;
}
public String getDailyWorkout() {
return "Do cardio daily!!";
}
public String getDailyFortune() {
return fortuneService.getFortune();
}
}

and the configuration file is

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.app"/></beans>

Field Injection:

package com.app;import org.springframework.stereotype.Component;@Component
public class FortuneService{
public String getFortune() {
return "Today is your lucky day!";
}
}

Here is a class that has the getFortune() method and we want to use this method in another class.

package com.app;import org.springframework.stereotype.Component;
import org.springframework.beans.factory.annotation.Autowired;
@Component
public class Coach {
@Autowired
private FortuneService fortuneService;
public String getDailyWorkout() {
return "Do cardio daily!!";
}
public String getDailyFortune() {
return fortuneService.getFortune();
}
}

and the configuration file is

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><context:component-scan base-package="com.app"/></beans>

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

Y.A.L.A.M.F.A.T.D.B.P. (Yet Another Legos As Metaphor For Agile and Technical Debt Blog Post)

Sneak Peek: what’s going to be new in Realm-Java 2.4.0 (or 3.0.0?)

What is BigData?

Skip unnecessary builds on GitHub actions

Step by step guide to migrate project from svn to git repository

Angrymals NFT ⚡ NFTbolt.io

Angrymals NFT ⚡

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Akshay Tomar

Akshay Tomar

More from Medium

Elasticsearch and Lucene

Caching- Is it necessary for an Application?

Utkarsh Shukla

Microservice, My Mistake In Designing A Web Service

SOLID Design Principles