Weekend -Special offer upto 50% off | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Dependency Injection
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. The subelement of is utilized for constructor infusion. Here we will injectprimitiveSubordinate protest (contained question)Collection values.Infusing primitiveHow about we see the straightforward case to infuse primitive and string-based esteems. We have made three documents here:javaapplicationContext.xmlTest.java
Constructor 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
Every gathering can have string based and non-string based esteems.In this case, we are taking the case of Forum where One inquiry can have different answers.
  1. Question.java
  2. applicationContext.xml
  3. 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:class="com.javaspot.Employee">
class="com.javaspot.Address"> class="com.javaspot.Employee" parent="e1"> Test.java: package com.javaspot;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;public class Test {public static void main(String[] args) {Resource r=new ClassPathResource("applicationContext.xml");BeanFactory factory=new XmlBeanFactory(r);Employee e1=(Employee)factory.getBean("e2");e1.show();}}
 Dependency Injection by setter method
We can infuse the reliance by setter strategy too. The subelement of is utilized for setter infusion. Here we will infuse
  1. primitive and String-based values
  2. Dependent object (contained object)
  3. Collection values etc.
Injecting primitive and string-based values by setter method
  • 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:class="com.javaspot.Address">
class="com.javaspot.Employee">Test.java:package com.javaspot;import org.springframework.beans.factory.BeanFactory;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;public class Test {public static void main(String[] args) {Resource r=new ClassPathResource("applicationContext.xml");BeanFactory factory=new XmlBeanFactory(r);Employee e=(Employee)factory.getBean("obj");e.displayInfo();}}
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.
  1. Question.java
  2. applicationContext.xml
  3. 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
Autowiring Modes:Autowiring
Example of Autowiring:
.class="org.sssit.A" autowire="byName">
  1. B.java
  2. A.java
  3. applicationContext.xml
  4. Test.java
 B.java: package org.sssit;public class B {B(){System.out.println("b is created");}void print(){System.out.println("hello b");}} A.java:package org.sssit;public class A {B b;A(){System.out.println("a is created");}public B getB() {return b;}public void setB(B b) {this.b = b;}void print(){System.out.println("hello a");}void display(){print();b.print();}}applicationContext.xml:class="org.sssit.B">class="org.sssit.A" autowire="byName"> Test.java:package org.sssit;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");A a=context.getBean("a",A.class);a.display();}}Output:b is createda is createdhello ahello b1) byName autowiring modeclass="org.sssit.B">class="org.sssit.A" autowire="byName">class="org.sssit.B">class="org.sssit.A" autowire="byName">2) byType autowiring modeclass="org.sssit.B">class="org.sssit.A" autowire="byType">class="org.sssit.B">class="org.sssit.B">class="org.sssit.A" autowire="byName">3) constructor autowiring modeclass="org.sssit.B">class="org.sssit.A" autowire="constructor">4) no autowiring modeclass="org.sssit.B">class="org.sssit.A" autowire="no">