Dependency Injection and IoC

Types of Configuration

Dependency Injection

Dependency Injection is a design pattern that implements the inversion of control principle for resolving dependencies. It allows the programmer to remove hard-coded dependencies so that the application becomes loosely coupled. A loosely coupled system is one in which each of its components has very little or no knowledge of other components present in the system.

public class Student {
private Address address;
public Student() {
address = new Address();
}
}

Types of Dependency Injection

Constructor-based Dependency Injection: Constructor-based dependency injection is the process of passing the dependency to a dependent object via a constructor. Consider an example. There are two classes Student and Address. In the Spring configuration file, we define the Address bean and pass this as an argument in the Student class using the constructor argument element.

// student.javapackage com.example;public class Student {
private String name;
private String rollNo;
private String className;
private Address address;

public Student(Address address){
this.address = address;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRollNo() {
return rollNo;
}
public void setRollNo(String rollNo) {
this.rollNo = rollNo;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public Address getAddress() {
return address;
}

}
// Address.javapackage com.example;

public class Address {
private String addLine;
private String city;
private String state;
private String country;

public String getAddLine() {
return addLine;
}
public void setAddLine(String addLine) {
this.addLine = addLine;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}

}
<?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-3.0.xsd">

<bean id="student" class="com.example.Student">

</bean>

<bean id="address" class="com.example.Address">
</bean>

</beans>
package com.example;

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

public class Test {
public static void main(String[] args) {
//Get ApplicationContext using spring configuration file.
ApplicationContext context =
new ClassPathXmlApplicationContext("applicationContext.xml");
//Get Student bean object from ApplicationContext instance.
Student student = (Student) context.getBean("student");
// use student object to call all get methods from Student class//Get Address from Student Object.
Address studentAddress = student.getAddress();
// use studentAddress object to call get methods from Address class
Configuration of XML file for setter injection
<bean id="myFortuneService" class="com.luv2code.springdemo.HappyFortuneService">
</bean>
<bean id="myCricketCoach" class ="com.luv2code.springdemo.CricketCoach"<property name="fortuneService" ref="myFortuneService" /></bean>

Some annotations used in Spring Annotation-based container configuration

@Required: It's a method-level annotation and applied to the setter method of a bean. This annotation simply indicates that the setter method must be configured to be dependency injected with a value at configuration time.

Conclusion

This article covers the basic terms related to IoC and Dependency Injection. For more information refer https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#spring-core.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store