
Dependency Injection :
Injection (DI) is an outline design that expels the dependency from the programming code with the goal that it can be anything but difficult to oversee and test the application. Reliance Injection makes our programming code inexactly coupled. To comprehend the DI better, Let's comprehend the Dependency Lookup (DL) first.
Dependency Lookup:
A obj = new AImpl();Factory Method:A obj = A.getA();Problems of Dependency Lookup:
Tight Coupling: The dependency query approach makes the code firmly coupled. On the off chance that asset is transformed, we have to play out a considerable measure of adjustment in the code.
Not easy for testing: This approach makes a considerable measure of issues while testing the application particularly in discovery testing.
Dependency Injection:
The Dependency Injection is an outline design that evacuates the reliance of the projects. In such case we give the data from the outside source, for example, XML document. It makes our code inexactly coupled and less demanding for testing. In such case we compose the code
class Employee{Address address;Employee(Address address){this.address=address;}public void setAddress(Address address){this.address=address;}}JAVA Tutorial Video
[embed]https://www.youtube.com/watch?v=Mb8nG-NH6gg&t=2835s[/embed]Two ways to perform Dependency Injection- By Constructor
- By Setter method
Dependency Injection by Constructor:
We can infuse the reliance by constructor. TheConstructor Injection with Dependent Object:
On the off chance that there is HAS-A connection between the classes, we make the occurrence of ward protest (contained question) first at that point pass it as a contention of the fundamental class constructor. Here, our situation is Employee HAS-An Address. The Address class question will be named as the needy protest.
infuse accumulation esteems by constructor in spring structure. There can be utilized three components inside the constructor-arg component.
It can be:- list
- set
- outline
- Question.java
- applicationContext.xml
- Test.java
Constructor Injection with Map
we are utilizing map as the appropriate response that have reply with posted username. Here, we are utilizing key and esteem combine both as a string.Like past illustrations, it is the case of discussion where one inquiry can have different answers.Inheriting Bean
By utilizing the parent characteristic of bean, we can indicate the legacy connection between the beans. In such case, parent bean esteems will be acquired to the present bean.We should see the basic case to acquire the bean.Employee.java This class contains three properties, three constructor and show() strategy to show the qualities.package com.javaspot;public class Employee {private int id;private String name;private Address address;public Employee() {}public Employee(int id, String name) {super();this.id = id;this.name = name;}public Employee(int id, String name, Address address) {super();this.id = id;this.name = name;this.address = address;}void show(){System.out.println(id+" "+name);System.out.println(address);}} Address.java:package com.javaspot;public class Address {private String addressLine1,city,state,country;public Address(String addressLine1, String city, String state, String country) {super();this.addressLine1 = addressLine1;this.city = city;this.state = state;this.country = country;}public String toString(){return addressLine1+" "+city+" "+state+" "+country;}} applicationContext.xml:Dependency Injection by setter method
We can infuse the reliance by setter strategy too. The- primitive and String-based values
- Dependent object (contained object)
- Collection values etc.
- java
- xml
- java
Setter Injection with Dependent Object
Like Constructor Injection, we can infuse the reliance of another bean utilizing setters. In such case, we utilize property component. Here, our situation is Employee HAS-An Address. The Address class protest will be named as the needy question. How about we see the Address class first:Address.java This class contains four properties, setters and getters and toString() technique.package com.javaspot;public class Address {private String addressLine1,city,state,country;//getters and setterspublic String toString(){return addressLine1+" "+city+" "+state+" "+country;}Employee.java:package com.javaspot;public class Employee {private int id;private String name;private Address address;//setters and gettersvoid displayInfo(){System.out.println(id+" "+name);System.out.println(address);}}applicationContext.xml:Setter Injection with Collection
We can infuse accumulation esteems by setter strategy in spring system. There can be utilized three components inside the property component.It can be:listsetmapEvery accumulation can have string based and non-string based esteems.In this case, we are taking the case of Forum where One inquiry can have various answers.- Question.java
- applicationContext.xml
- Test.java
Setter Injection with Map:
In this illustration, we are utilizing map as the response for an inquiry that have reply as the key and username as the esteem. Here, we are utilizing key and esteem combine both as a string. Like past illustrations, it is the case of discussion where one inquiry can have different answers.Question.java This class contains three properties, getters and setters and displayInfo() strategy to show the data.Autowiring:
Autowiring highlight of spring structure empowers you to infuse the protest reliance verifiably. It inside utilizations setter or constructor infusion.Autowiring can't be utilized to infuse primitive and string esteems. It works with reference as it were.Favorable position of Autowiring It requires the less code since we don't have to compose the code to infuse the reliance unequivocally.Disadvantage:- No Program Control
- Cannot be used String and primitive

Example of Autowiring:
.- B.java
- A.java
- applicationContext.xml
- Test.java