Dependency Injection using Core Java | JavaInUse



Dependency Injection using Core Java



Overview

In the previous chapter we saw the traditional approach of instantiating the dependencies when needed using the new keyword. Inversion of Control(IOC) is a design pattern which suggests to determine, instantiate and provide the dependencies of a class beforehand.Thus Inversion of control pattern inverts the responsibility of managing life cycle of object. IOC can be implemented using Dependency injection. In this chapter we will modify the Employee Management System implemented in previous chapter to use Dependency Injection. As we saw in the previous chapter EmployeeService class has a dependency of EmployeeDAO class, which is implemented using the new keyword. Here we will insert this dependency using Dependency Injection. This chapter makes use of Core Java to explain Dependency Injection Principles. Dependency Injection using Spring is implemented in next chapter.

Lets Begin

We will create Eclipse Maven project as follows-

Dependency injection can be implemented in java in 3 ways-
1. Setter Injection.
2. Constructor Injection
3. Interface Injection

Spring uses only Setter and Constructor Injection so we will only look at these. We will first have a look at setter injection.

Dependency Injection using Setter Injection.

For Setter Injection we will have to create the setters for all dependencies required.
EmployeeService.java has dependency of EmployeeDAO.java.
Unlike in chapter2, we will modify the EmployeeService.java to remove instantiation of EmployeeDAO.java using the new keyword.
Also we will add setter for EmployeeDAO instance.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.javainuse</groupId>
  <artifactId>employee-management-system</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.0.5.RELEASE</version>
		</dependency>
	</dependencies>
</project>
Next we will insert the instance of EmployeeDAO.java in EmployeeService using Setter Injection.The code
empService.setEmpDAO(empDAO); inserts the dependency in Employee Service using Setter dependency Injection.

package com.javainuse.main;

import java.util.List;

import com.javainuse.dao.EmployeeDAO;
import com.javainuse.domain.Employee;
import com.javainuse.service.EmployeeService;

public class MainApplication {

	public static void main(String[] args) {

		EmployeeDAO empDAO = new EmployeeDAO();
		EmployeeService empService = new EmployeeService();
		//insert dependency using setter
		empService.setEmpDAO(empDAO);
		Employee emp1 = new Employee("1", "Test1", "Manager", 1000);
		Employee emp2 = new Employee("1", "Test2", "Manager", 1000);
		Employee emp3 = new Employee("1", "Test3", "Manager", 1000);
		Employee emp4 = new Employee("1", "Test4", "Manager", 1000);
		Employee emp5 = new Employee("1", "Test5", "Manager", 1000);

		empService.addNewEmployee(emp1);
		empService.addNewEmployee(emp2);
		empService.addNewEmployee(emp3);
		empService.addNewEmployee(emp4);
		empService.addNewEmployee(emp5);
		List<Employee> employees = empService.getEmployees();
		for (Employee employee : employees) {
			System.out.println(employee.getName());
		}

	}

}

When we run the program we get the output as

Dependency Injection using Constructor Injection.

For Constructor Injection we will have to create the constructor for all dependencies required.
Unlike in chapter2, we will modify the EmployeeService.java to remove instantiation of EmployeeDAO.java using the new keyword. Also we will add Constructor for EmployeeService which takes the argument EmployeeDAO instance.
package com.javainuse.service;

import java.util.List;

import com.javainuse.dao.EmployeeDAO;
import com.javainuse.domain.Employee;

public class EmployeeService {
	EmployeeDAO empDAO;

	public EmployeeService() {
	}
	
	//Constructor for EmployeeService for constructor injection
	public EmployeeService(EmployeeDAO empDAO) {
		super();
		this.empDAO = empDAO;
	}

	public void addNewEmployee(Employee emp) {
		empDAO.addNewEmployee(emp);
	}

	public List<Employee> getEmployees() {
		return empDAO.getAllEmployees();
	}

}

Next we will insert the instance of EmployeeDAO.java in EmployeeService using Constructor Injection. The code
new EmployeeService(empDAO); inserts the dependency in Employee Service using Constructor Injection.

package com.javainuse.main;

import java.util.List;

import com.javainuse.dao.EmployeeDAO;
import com.javainuse.domain.Employee;
import com.javainuse.service.EmployeeService;

public class MainApplication {

	public static void main(String[] args) {

		EmployeeDAO empDAO = new EmployeeDAO();
		//insert dependency using Constructor injection
		EmployeeService empService = new EmployeeService(empDAO);
		Employee emp1 = new Employee("1", "Test1", "Manager", 1000);
		Employee emp2 = new Employee("1", "Test2", "Manager", 1000);
		Employee emp3 = new Employee("1", "Test3", "Manager", 1000);
		Employee emp4 = new Employee("1", "Test4", "Manager", 1000);
		Employee emp5 = new Employee("1", "Test5", "Manager", 1000);

		empService.addNewEmployee(emp1);
		empService.addNewEmployee(emp2);
		empService.addNewEmployee(emp3);
		empService.addNewEmployee(emp4);
		empService.addNewEmployee(emp5);
		List<Employee> employees = empService.getEmployees();
		for (Employee employee : employees) {
			System.out.println(employee.getName());
		}

	}

}

When we run the program we get the output as

Advantages of Dependency Injection using core java-