Traditional approach without using Dependency Injection | JavaInUse

Traditional approach without using Dependency Injection


In the previous chapter we saw how to create a Maven project with required dependencies which will be used throughout this series. Before we start with Spring lets understand the Traditional approach of Development. In traditional programming approach the dependencies of a class are determined and instantiated at runtime as and when they are required. Thus when need arises we instantiate the dependency class using the new keyword. Using this approach has many disadvantages which we will see later.

Here we will implement an Employee Management system. The design will be as follows.

The user can perform the functions of adding new employee, displaying all employees.
We will implement this system using Traditional approach.

Lets Begin

We will create Eclipse Maven project as follows-

We will first create our Domain Class as follows
package com.javainuse.domain;

public class Employee {

    private String empId;
    private String name;
    private String designation;
    private double salary;

    public Employee() {

    public Employee(String empId, String name, String designation, double salary) {
        this.setEmpId(empId); = name;
        this.designation = designation;
        this.salary = salary;

    public String getName() {
        return name;

    public void setName(String name) { = name;

    public String getDesignation() {
        return designation;

    public void setDesignation(String designation) {
        this.designation = designation;

    public double getSalary() {
        return salary;

    public void setSalary(double salary) {
        this.salary = salary;

    public String getEmpId() {
        return empId;

    public void setEmpId(String empId) {
        this.empId = empId;

Next we will write The has a dependency on the So we create an instance of the EmployeeDAO class in the using the new keyword. The function of the EmployeeDAO class is to store employee details in DB and fetch these details when required from DB.

The has two methods.
  • addNewEmployee(Employee emp)- This method calls the EmployeeDAO class method to save the new Employee details.
  • getEmployees()- This method calls the EmployeeDAO class method to get the List of all employees.
package com.javainuse.service;

import java.util.List;
import com.javainuse.dao.EmployeeDAO;
import com.javainuse.domain.Employee;

public class EmployeeService {
    //insert dependency using new keyword
    EmployeeDAO empDAO = new EmployeeDAO();    

    public void addNewEmployee(Employee emp) {

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

Currently in this chapter we will not use any DB. We will mock the as follows to save the employee details in an Employee ArrayList and return the same. So our will be as follows-
package com.javainuse.dao;

import java.util.ArrayList;
import java.util.List;
import com.javainuse.domain.Employee;

public class EmployeeDAO {

    private List<Employee> testEmployees = new ArrayList<Employee>();

    public void addNewEmployee(Employee employee) {

    public List<Employee> getAllEmployees() {
        return new ArrayList<Employee>(testEmployees);

Finally we will write to run our program. We create new instances of Employee and add it to the new instance of EmployeeService.
package com.javainuse.main;

import java.util.List;
import com.javainuse.domain.Employee;
import com.javainuse.service.EmployeeService;

public class MainApplication {

    public static void main(String[] args) {
        EmployeeService empService = new EmployeeService();
        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);

        List<Employee>employees = empService.getEmployees();
        for (Employee employee : employees) {


On Running this program we get the output as

Disadvantages of this traditional approach-

  • The main disadvantage of this approach is the tight coupling between the classes. The EmployeeService and the
    EmployeeDAO are tightly coupled. If the EmployeeDAO is suppose replaced by another class in future then there will be code changes required in the EmployeeService class
  • The code becomes difficult to analyze as new user will have to check where all the instances are created for the dependencies.
  • Testing of the code is cumbersome since we cannot mock the classes for testing purpose.
  • Spring provides support for different frameworks like Hibernate which makes it easier to implement these frameworks. With traditional approach this is not the case.

Download Source Code

Download it - Employee Management System

What Next?

In the next chapter will implement Dependency Injection using Core Java.