Skip to main content

Dependency Injection

What is Dependency Injection?
=======================================
-> Dependency Injection (DI) is a design pattern that removes the dependency from the programming code so that it can be easy to manage and test the application.
-> Dependency Injection makes our programming code loosely coupled.
-> In such case we provide the information from the external source such as XML file.
-> It makes our code loosely coupled and easier for testing.
-> DI is all about objects and their dependencies.
-> In Spring frameowork, Dependency Injection (DI) design pattern is used to define the object dependencies between each other.
-> It is a process where objects are defining their dependencies and other objects work with them either constructor arguments or properties .
-> There are 2 widely used types: 1. Setter Injection, and 2. Constructor Injection
-> If we configure both setter injection and constructor injection on bean property the values given by setter injection will remain as final values of injection
     because setter(-) method executes after constructor executes and overrides the values injected by the constructor execution or injection.


-> When we go for Setter Injection or when we go for Constructor Injection?
====================================
-> At the time of creating our target class object, the dependent objects are injected or instantiated through Constructor Injection.
-> Whereas in Setter Injection dependent objects are not injected or instantiated while creating the target class object;
     those will be injected after the target class has been instantiated by calling the setter on the target object.
-> If bean class contains only one property or if all the property of bean class should participate in dependency injection then
     we should go for constructor injection because constructor injection is very faster than setter injection.
-> If bean class contains more than one property and there is no need of making all properties (means optional to inject) are
     participating in dependency injection, then we should go for setter injection. So it is bit delayed injection.
-> Constructor Injection doesn’t support cyclic dependency.
-> Whereas Setter injection supports cyclic or circular dependency injection.
-> To perform constructor injection on n properties in all angles, n! Constructors are required.
-> Whereas to perform setter injection on n properties in all angles “n-setter” methods are required.
 -> To perform constructor injection we will use <constructor-arg>.
-> Whereas to perform setter injection we will use <property> tag.


-> Spring supports dependency injection on the following properties:
    a. Simple properties (primitive data types, String)
    b. Reference or Object type properties
    c. Collection properties (Array, List, Set, Map, Properties, and etc...)

Comments

Popular posts from this blog

Mockito interview Questions

1.       Question 1. What Is Mockito? Answer : Mockito allows creation of mock object for the purpose of Test Driven Development and Behavior Driven development. Unlike creating actual object, Mockito allows creation of fake object (external dependencies) which allows it to give consistent results to a given invocation. 2.       Question 2. Why Do We Need Mockito? What Are The Advantages? Answer : Mockito differentiates itself from the other testing framework by removing the expectation beforehand. So, by doing this, it reduces the coupling. Most of the testing framework works on the "expect-run-verify". Mockito allows it to make it "run-verify" framework. Mockito also provides annotation which allows to reduce the boilerplate code. 3.       Question 3. Can You Explain A Mockito Framework? Answer : In Mockito, you always check a particular class. The dependency in that class is injected using m...

JAVA Expert Interview Questions Answers 2017

Java Basics ::  Interview Questions and Answers Home  »  Interview Questions  »  Technical Interview  »  Java Basics  » Interview Questions 1.     What is the difference between a constructor and a method? A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator. A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator. 2.     What is the purpose of garbage collection in Java, and when is it used? The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. 3.  ...

Java Example Program to Convert List to Set

import java.util.ArrayList; import java.util.HashSet; import java.util.Set; public class ListToSet {  /**   * @author Amarjit Kumar   * @category interview questions   *   * Description: Convert List to set in java with example program   *   */  public static void main(String[] args) {   ArrayList<String> arrList= new ArrayList<>();   arrList.add("Java");   arrList.add("Java");   arrList.add("List to String");   arrList.add("Example Program");   Set<String> strSet = new HashSet<String>(arrList);   System.out.println(strSet);   System.out.println(arrList);  } } /*  * Java program to convert list to set. Convert ArrayList of string to HashSet  * in java example program How to convert List to Set in java Set<String> strSet  * = new HashSet<String>(arrList); HashSet having a constructor which will take  * list as an ar...