How to create your own dependency injection framework in Java

Posted on



Overview

This article will guide you to understand and build a lightweight Java application using your own Dependency Injection implementation.

Dependency Injection … DI… Inversion Of Control…IoC, I guess you might have heard these names so many times while your regular routine or specially interview preparation time that you wonder what exactly it is.

But if you really want to understand how internally it works then continue reading here.



So, what is dependency injection?

Dependency injection is a design pattern used to implement IoC, in which instance variables (ie. dependencies) of an object got created and assigned by the framework.

To use DI feature a class and it’s instance variables just need to add annotations predefined by the framework.

The Dependency Injection pattern involves 3 types of classes.

  • Client Class: The client class (dependent class) depends on the service class.
  • Service Class: The service class (dependency class) that provides service to the client class.
  • Injector Class: The injector class injects the service class object into the client class.

In this way, the DI pattern separates the responsibility of creating an object of the service class out of the client class. Below are a couple more terms used in DI.

  • Interfaces that define how the client may use the services.
  • Injection refers to the passing of a dependency (a service) into the object (a client), this is also referred to as auto wire.



So, what is Inversion of Control?

In short, “Don’t call us, we’ll call you.”

  • Inversion of Control (IoC) is a design principle. It is used to invert different kinds of controls (ie. object creation or dependent object creation and binding ) in object-oriented design to achieve loose coupling.
  • Dependency injection one of the approach to implement the IoC.
  • IoC helps to decouple the execution of a task from implementation.
  • IoC helps it focus a module on the task it is designed for.
  • IoC prevents side effects when replacing a module.


Class diagram of dependency injection design pattern:

Class diagram of dependency injection design pattern

In the above class diagram, the Client class that requires UserService and AccountService objects does not instantiate the UserServiceImpl and AccountServiceImpl classes directly.

Instead, an Injector class creates the objects and injects them into the Client, which makes the client independent of how the objects are created.



Types of Dependency Injection

  • Constructor injection: the injector supplies the service (dependency) through the client class constructor. In this case, Autowired annotation added on the constructor.
  • Property injection: the injector supplies the service (dependency) through a public property of the client class. In this case Autowired annotation added while member variable declaration.
  • Setter method injection: the client class implements an interface that declares the method(s) to supply the service (dependency) and the injector uses this interface to supply the dependency to the client class.

In this case, Autowired annotation added while method declaration.



How it works?

To understand Dependency Injection implementation, refer code snippets here, or download/clone the tutorial shared here on GitHub.



Prerequisite

For a better understanding of this tutorial, it’s good to have basic knowledge of Annotations and reflection in advance.



Required Java libraries

Before begin with the coding steps, you can create new maven project in the eclipse and add Burningwave Core dependency in pom.xml:

<dependency>
    <groupId>org.burningwave</groupId>
    <artifactId>core</artifactId>
    <version>12.38.3</version>
</dependency>

To use Burningwave Core as a Java module, add the following to your module-info.java:

requires io.github.toolfactory.narcissus;
requires org.burningwave.core;



Create user-defined annotations

As described above DI implementation has to provide predefined annotations, which can be used while declaration of client class and service variables inside a client class.

Let’s add basic annotations, which can be used by client and service classes:


package org.di.framework.annotations;
import java.lang.annotation.*;
/**
* Client class should use this annotation
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {
}

view raw

Component.java

hosted with ❤ by GitHub


package org.di.framework.annotations;
import java.lang.annotation.*;
import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* Service field variables should use this annotation
*/
@Target({ METHOD, CONSTRUCTOR, FIELD })
@Retention(RUNTIME)
@Documented
public @interface Autowired {
}

view raw

Autowired.java

hosted with ❤ by GitHub


package org.di.framework.annotations;
import java.lang.annotation.*;
/**
* Service field variables should use this annotation
* This annotation Can be used to avoid conflict if there are multiple implementations of the same interface
*/
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.TYPE, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Qualifier {
String value() default “”;
}

view raw

Qualifier.java

hosted with ❤ by GitHub



Service interfaces