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

Two ways to perform Dependency Injection

  • By Constructor
  • By Setter method
Dependency Injection by Constructor:

We can infuse the reliance by constructor. The <constructor-arg> subelement of <bean> is utilized for constructor infusion. Here we will inject

primitive

Subordinate protest (contained question)

Collection values.

Infusing primitive

How about we see the straightforward case to infuse primitive and string-based esteems. We have made three documents here:

java

applicationContext.xml

Test.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:

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans

xmlns=”http://www.springframework.org/schema/beans”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xmlns:p=”http://www.springframework.org/schema/p”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

<bean id=”e1″ class=”com.javaspot.Employee”>

<constructor-arg value=”101″></constructor-arg>

<constructor-arg  value=”Sachin”></constructor-arg>

</bean>

<bean id=”address1″ class=”com.javaspot.Address”>

<constructor-arg value=”21,Lohianagar”></constructor-arg>

<constructor-arg value=”Ghaziabad”></constructor-arg>

<constructor-arg value=”UP”></constructor-arg>

<constructor-arg value=”USA”></constructor-arg>

</bean>

 

<bean id=”e2″ class=”com.javaspot.Employee” parent=”e1″>

<constructor-arg ref=”address1″></constructor-arg>

</bean>

</beans>

 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 <property> subelement of <bean> 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 setters

public 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 getters

void displayInfo(){

System.out.println(id+” “+name);

System.out.println(address);

}

}

applicationContext.xml:

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans

xmlns=”http://www.springframework.org/schema/beans”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xmlns:p=”http://www.springframework.org/schema/p”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

<bean id=”address1″ class=”com.javaspot.Address”>

<property name=”addressLine1″ value=”51,Lohianagar”></property>

<property name=”city” value=”Ghaziabad”></property>

<property name=”state” value=”UP”></property>

<property name=”country” value=”India”></property>

</bean>

<bean id=”obj” class=”com.javaspot.Employee”>

<property name=”id” value=”1″></property>

<property name=”name” value=”Sachin Yadav”></property>

<property name=”address” ref=”address1″></property>

</bean>

</beans>

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:

list

set

map

Every 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:

.<bean id=”a” class=”org.sssit.A” autowire=”byName”></bean>

  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:

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans

xmlns=”http://www.springframework.org/schema/beans”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xmlns:p=”http://www.springframework.org/schema/p”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

<bean id=”b” class=”org.sssit.B”></bean>

<bean id=”a” class=”org.sssit.A” autowire=”byName”></bean>

</beans>

 

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 b

1) byName autowiring mode

<bean id=”b” class=”org.sssit.B”></bean>

<bean id=”a” class=”org.sssit.A” autowire=”byName”></bean>

<bean id=”b1″ class=”org.sssit.B”></bean>

<bean id=”a” class=”org.sssit.A” autowire=”byName”></bean>

2) byType autowiring mode

<bean id=”b1″ class=”org.sssit.B”></bean>

<bean id=”a” class=”org.sssit.A” autowire=”byType”></bean>

<bean id=”b1″ class=”org.sssit.B”></bean>

<bean id=”b2″ class=”org.sssit.B”></bean>

<bean id=”a” class=”org.sssit.A” autowire=”byName”></bean>

3) constructor autowiring mode

<bean id=”b” class=”org.sssit.B”></bean>

<bean id=”a” class=”org.sssit.A” autowire=”constructor”></bean>

4) no autowiring mode

<bean id=”b” class=”org.sssit.B”></bean>

<bean id=”a” class=”org.sssit.A” autowire=”no”></bean>

Drop Us A Query

Trending Courses
  • Python and Django Online Training
  • Python is a dynamic interrupted language which is used in wide varieties of applications. It is very interactive object oriented and high-level programming language.

  • Tableau Online Training
  • Tableau is a Software company that caters interactive data visualization products that provide Business Intelligence services. The company’s Head Quarters is in Seattle, USA.

  • MicroStrategy Online Training
  • Micro Strategy is one of the few independent and publicly trading Business Intelligence software provider in the market. The firm is operational in 27 Countries around the globe.

  • PEGA (PRPC) 7.2 Certification Online Training
  • Pega Systems Inc. is a Cambridge, Massachusetts based Software Company. It is known for developing software for Customer Relationship Management (CRM) and Business process Management (BPM).

  • Workday Online Training
  • Workday specialises in providing Human Capital Management, Financial Management and payroll in online domain.It is a major web based ERP software vendor.

  • Power BI Online Training
  • Power BI is business analytics service by Microsoft. With Power BI, end users can develop reports and dashboards without depending on IT staff or Database Administrator.


100% Secure Payments. All major credit & debit cards accepted.