Spring Dependency Injection

DI(Dependency injection) is a very big concept in spring. Which spring inherits it from JEE with reference to JSR 330 which makes us to maximize reusability, testability and maintainability of Java code. Before dependency injection has come into picture developers need to write boiler plate code like below,

There is a class named Address.java and Person.java the Person.java will take Address.java as property were Person will return all of it state with to String. Lol! you thought that what is the problem with it? there is a problem when we are developing a large enterprise application because there are lots of .java files and there will be lots of dependencies between them in project in that case we are unable to manage that level of dependency with boilerplate DI, so we need some thing super!

Boilerplate DI

Address.java

package com.sourceforopensource.Pojos;

public class Address {
 private String street;
 private String postcode;
 
 public Address(String street, String postcode) {
 this.street = street;
 this.postcode = postcode;
 }

 @Override
 public String toString() {
 return "Address [street=" + street + ", postcode=" + postcode + "]";
 }
 
 
}


Person.java



package com.sourceforopensource.Pojos;

public class Person {
 
 private int id;
 private String name;
 private int taxId;
 private Address address;
 
 

 public Person() {
 
 }
 
 public Person(int id, String name) {
 this.id = id;
 this.name = name;
 }

 public void speak() {
 System.out.println("Hello! I'm a person.");
 }

 public void setTaxId(int taxId) {
 this.taxId = taxId;
 }

 public void setAddress(Address address) {
 this.address = address;
 }

 @Override
 public String toString() {
 return "Person [id=" + id + ", name=" + name + ", taxId=" + taxId
 + ", address=" + address + "]";
 }

 
}


App.java(client)


package com.sourceforopensource.Pojos;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class App {

 public static void main(String[] args) {
 Person p = new Person();
 p.setAddress(new Address(" Arabic college road","560045" ));
 
 System.out.println(p);
 }

}


The above style of coding is before spring coding!

output: Person [id=0, name=null, taxId=0, address=Address [street= Arabic college road, postcode=560045

After Spring came we talk it as an era for simplicity, lets see how the above problem can be solved with spring DI

Address.java


package com.sourceforopensource.pojos;

public class Address {
private String street;
private String postcode;

public Address(String street, String postcode) {
this.street = street;
this.postcode = postcode;
}

@Override
public String toString() {
return "Address [street=" + street + ", postcode=" + postcode + "]";
}
}

Person.java


package com.sourceforopensource.pojos;

public class Person {

private int id;
private String name;
private int taxId;
private Address address; //the person.java need address.java which we provide it with ref="address" attribute in beans.xml

public Person() {

}

public Person(int id, String name) {
this.id = id;
this.name = name;
}

public void speak() {
System.out.println("Hello! I'm a person.");
}

public void setTaxId(int taxId) {
this.taxId = taxId;
}

public void setAddress(Address address) {
this.address = address;
}

@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + ", taxId=" + taxId
+ ", address=" + address + "]";
}
}

beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person"
class="com.sourceforopensource.pojos.Person">
<constructor-arg value="777" name="id" type="int">
</constructor-arg>
<constructor-arg value="Mary" name="name" type="String"></constructor-arg>
<property value="321" name="taxId">
</property>
<property name="address" ref="address"></property>//here we are passing address to person
</bean>

<bean id="address" class="com.sourceforopensource.pojos.Address">
<constructor-arg name="street" value="1 Church Lane"></constructor-arg>
<constructor-arg name="postcode" value="9876"></constructor-arg>
</bean>

</beans>

App.java


package com.sourceforopensource.pojos;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class App {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("com/sourceforopensource/pojos/beans.xml");

Person person = (Person)context.getBean("person");
person.speak();

Address address = (Address)context.getBean("address");
System.out.println(address);

System.out.println(person);

((ClassPathXmlApplicationContext)context).close();
}

}

Balakrishna Pendyala

Author: Balakrishna Pendyala

Balakrishna is a Software Engineer working in Media Streaming Domain, and also worked on Banking and Health Care domains. His areas of interest include Object Oriented Design, SOLID Design principles, Restful Web Services and Open Source softwares including Spring, Spring Boot, and Hibernate.

If you found an error, highlight it and press Shift + Enter or click here to inform us.

Leave a Reply

Your email address will not be published. Required fields are marked *