How to do the dependency injection without using annotation in java and usages of DI




Every Java-based application has a few objects that work together to present what the end-user sees as a working application. When writing a complex Java application, application classes should be as independent as possible of other Java classes to increase the possibility to reuse these classes and to test them independently of other classes while unit testing. Dependency Injection (or sometime called wiring) helps in gluing these classes together and at the same time keeping them independent.
Consider you have an application which has a text editor component and you want to provide a spell check. Your standard code would look something like this −
public class TextEditor {
   private SpellChecker spellChecker;
   
   public TextEditor() {
      spellChecker = new SpellChecker();
   }
}
What we've done here is, create a dependency between the TextEditor and the SpellChecker. In an inversion of control scenario, we would instead do something like this −
public class TextEditor {
   private SpellChecker spellChecker;
   
   public TextEditor(SpellChecker spellChecker) {
      this.spellChecker = spellChecker;
   }
}
Here, the TextEditor should not worry about SpellChecker implementation. The SpellChecker will be implemented independently and will be provided to the TextEditor at the time of TextEditor instantiation. This entire procedure is controlled by the Spring Framework.
Here, we have removed total control from the TextEditor and kept it somewhere else (i.e. XML configuration file) and the dependency (i.e. class SpellChecker) is being injected into the class TextEditor through a Class Constructor. Thus the flow of control has been "inverted" by Dependency Injection (DI) because you have effectively delegated dependances to some external system.
The second method of injecting dependency is through Setter Methods of the TextEditor class where we will create a SpellChecker instance. This instance will be used to call setter methods to initialize TextEditor's properties.
Thus, DI exists in two major variants and the following two sub-chapters will cover both of them with examples −
Sr.No.Dependency Injection Type & Description
1Constructor-based dependency injection
Constructor-based DI is accomplished when the container invokes a class constructor with a number of arguments, each representing a dependency on the other class.
2Setter-based dependency injection
Setter-based DI is accomplished by the container calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
You can mix both, Constructor-based and Setter-based DI but it is a good rule of thumb to use constructor arguments for mandatory dependencies and setters for optional dependencies.
The code is cleaner with the DI principle and decoupling is more effective when objects are provided with their dependencies. The object does not look up its dependencies and does not know the location or class of the dependencies, rather everything is taken care by the Spring Framework.

Advantages of Dependency Injection

  • DI allows a client the flexibility of being configurable. Only client's behavior is fixed.

  • Testing can be performed using mock objects.

  • Loosely couple architecture.

  • DI advantages of high cohesion are:

    • Reduced module complexity

    • Increased system maintainability, because logic changes in the domain affect fewer modules.

    • Increased module reusability.

  • DI does not require any changes in code behavior it can be applied to legacy code as refactoring.

  • DI allows a client to remove all knowledge of a concrete implementation that needs to use. It is more reusable, more testable, more readable code.

  • DI makes it possible to eliminate, or at least reduce unnecessary dependencies.

  • DI allows concurrent or independent development.

  • DI decreases coupling between a class and its dependency.

Disadvantages of Dependency Injection

  • DI creates clients that demand configure details supplied by construction code.

  • DI can make code difficult to trace because it separates behavior from construction; this means developers refer to more files to follow how a system performs.

  • DI can cause an explosion of types, especially in languages that have explicit interface types like C# and Java.

  • DI can encourage dependence on DI framework.

  • Tight coupling :

    • A change in only one module usually forces a ripple effect of changes in other modules.

Dependency Injection

Dependency Injection (DI)

  • Dependency Injection (DI) is a software design pattern that implements inversion of control for resolving dependencies.

  • An injection is the passing of a dependency to a dependent object that would use it.

  • DI is a process whereby objects define their dependencies. The other objects they work with—only through constructor arguments or arguments to a factory method or property—are set on the object instance after it is constructed or returned from a factory method.

  • The container then injects those dependencies, and it creates the bean. This process is named Inversion of Control (IoC) (the bean itself controls the instantiation or location of its dependencies by using direct construction classes or a Service Locator).

  • DI refers to the process of supplying an external dependency to a software component.

Dependency Injection Performed Two Ways

1. Constructor-Based Dependency Injection

  • Constructor-based DI is when the container invokes a constructor with a number of arguments, each of which represents a dependency or other class.

  • Calling a static factory method with particular arguments to construct the bean is approximately equivalent, treating arguments to a constructor and to a static factory method. The following example shows a class that can only be dependency-injected with constructor injection. It is a POJO that has no dependencies on container specific interfaces, base classes, or annotations.

Example of Constructor-Based DI

Book.java
applicationContext.xml
Main.java
Output:

1 null

2. Setter-Based Dependency Injection

Setter-based DI is the when the container calls setter methods on your beans after it has invoked a no-argument constructor or no-argument static factory method to instantiate that bean.

The following example shows a class that can only have pure setter injection.

Example of Setter Based DI

Book.java
applicationContext.xml
Main.java
Output :
  • The Complete Reference J2EE  Herbert Schildt

Our Java application development team has just explained the concept of Dependency Injection, its advantages, disadvantages, and uses in Spring with examples. If you still have any confusion, tell us and get the answer from professionals.


For more => https://evisioncsse.blogspot.com/2020/01/java-bean.html


Spring ioc-containers

http://evisioncsse.blogspot.com/2022/03/spring-ioc-containers_17.html

Comments

Post a Comment

Popular posts from this blog

Hibernate (Java) -- by jps sasadara

Observer Design Pattern & RxJava & @Async

JAVA uml Based cording <<< by jps sasadara >>>