Spring IoC Employee Management System

JavaJavaBeginner
Practice Now

Introduction

In this project, you will learn how to build an employee management system using Spring IoC (Inversion of Control) container. The goal is to use the Spring IoC container to extract employee information from a configuration file and store it in a list collection, from which the employee information can be retrieved and printed.

👀 Preview

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.labex.TestEmployeeSystem
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl addEmployee
INFO: Entering addEmployee() method
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl addEmployee
INFO: Entering addEmployee() method
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl addEmployee
INFO: Entering addEmployee() method
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl addEmployee
INFO: Entering addEmployee() method
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl addEmployee
INFO: Entering addEmployee() method
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl getAllEmployees
INFO: Entering getAllEmployees() method
Employees [employeeId=000001, employeeName=John Doe, employeeSex=Male, employeeBirthday=1993-11-06, employeeHiredate=2018-10-11, userId=1]
Employees [employeeId=000021, employeeName=Jane Smith, employeeSex=Female, employeeBirthday=1990-07-16, employeeHiredate=2019-10-21, userId=4]
Employees [employeeId=000022, employeeName=Alice Johnson, employeeSex=Female, employeeBirthday=1993-02-11, employeeHiredate=2019-12-27, userId=12]
Employees [employeeId=000035, employeeName=Bob Brown, employeeSex=Male, employeeBirthday=1991-06-23, employeeHiredate=2020-05-06, userId=19]
Employees [employeeId=000066, employeeName=Mary Taylor, employeeSex=Female, employeeBirthday=1997-12-21, employeeHiredate=2021-01-03, userId=20]
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.499 sec

ðŸŽŊ Tasks

In this project, you will learn:

  • How to implement the IEmployeeService interface to handle employee-related operations
  • How to create the EmployeeServiceImpl class to provide the implementation of the IEmployeeService interface
  • How to configure the Spring IoC container to enable annotation-based package scanning
  • How to implement the test() method to retrieve employee information from the Spring IoC container and print it

🏆 Achievements

After completing this project, you will be able to:

  • Use the Spring IoC container to manage employee data
  • Implement service interfaces and their implementations using annotations
  • Configure the Spring IoC container using an XML configuration file
  • Retrieve and print employee information from the Spring IoC container

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java/ObjectOrientedandAdvancedConceptsGroup -.-> java/interface("`Interface`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") subgraph Lab Skills java/interface -.-> lab-300400{{"`Spring IoC Employee Management System`"}} java/oop -.-> lab-300400{{"`Spring IoC Employee Management System`"}} end

Implement the IEmployeeService Interface

In this step, you will learn how to implement the IEmployeeService interface. Follow the steps below to complete this step:

  1. Open the IEmployeeService.java file located in the EmployeeSystemIoC/src/main/java/org/labex/service directory.

  2. Declare two methods in the IEmployeeService interface:

    • addEmployee: This method takes an Employee object as a parameter and returns a boolean value. It is used to add the employee object to the list.
    • getAllEmployees: This method has no parameters and returns a List. It is used to retrieve all employee information.

Your IEmployeeService.java file should look like this:

package org.labex.service;

import org.labex.pojo.Employee;

import java.util.List;

public interface IEmployeeService {
    boolean addEmployee(Employee employee);

    List<Employee> getAllEmployees();
}
âœĻ Check Solution and Practice

Implement the EmployeeServiceImpl Class

In this step, you will learn how to implement the EmployeeServiceImpl class, which is the implementation of the IEmployeeService interface.

  1. Create a new file called EmployeeServiceImpl.java in the EmployeeSystemIoC/src/main/java/org/labex/service directory.

  2. Implement the EmployeeServiceImpl class and make it implement the IEmployeeService interface.

  3. Annotate the EmployeeServiceImpl class with the @Service annotation.

  4. Declare a static List collection globally and assign a collection object in the parameterless constructor.

  5. Implement the addEmployee() and getAllEmployees() methods:

    • In the addEmployee() method, log the execution process using the logger.info() method.
    • In the getAllEmployees() method, log the execution process using the logger.info() method.

Your EmployeeServiceImpl.java file should look like this:

package org.labex.service;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.labex.pojo.Employee;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class EmployeeServiceImpl implements IEmployeeService {

    private static List<Employee> employees;

    private static final Log logger  = LogFactory.getLog(EmployeeServiceImpl.class);

    public EmployeeServiceImpl() {
        employees = new ArrayList<>();
    }

    @Override
    public boolean addEmployee(Employee employee) {
        logger.info("Entering addEmployee() method");
        return employees.add(employee);
    }

    @Override
    public List<Employee> getAllEmployees() {
        logger.info("Entering getAllEmployees() method");
        return employees;
    }
}
âœĻ Check Solution and Practice

Configure the Spring IoC Container

In this step, you will learn how to configure the Spring IoC container to enable annotation-based package scanning.

  1. Open the applicationContext.xml file located in the EmployeeSystemIoC/src/main/resources directory.

  2. Add the following XML namespace declarations to the <beans> element:

    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context.xsd"
  3. Add the <context:component-scan> element to enable annotation-based package scanning for the org.labex package.

Your applicationContext.xml file should look like this:

<?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"
       xmlns:c="http://www.springframework.org/schema/c"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- Enable component scanning for annotations -->
    <context:component-scan base-package="org.labex"/>

    <!-- Employee Information -->
    <bean id="emp1" class="org.labex.pojo.Employee"
          c:employeeId="000001" c:employeeName="John Doe" c:employeeSex="Male"
          c:employeeBirthday="1993-11-06" c:employeeHiredate="2018-10-11" c:userId="1"/>

    <bean id="emp2" class="org.labex.pojo.Employee"
          c:employeeId="000021" c:employeeName="Jane Smith" c:employeeSex="Female"
          c:employeeBirthday="1990-07-16" c:employeeHiredate="2019-10-21" c:userId="4"/>

    <!-- Add more employee beans here -->

</beans>
âœĻ Check Solution and Practice

Implement the Test Method

In this step, you will learn how to implement the test() method in the TestEmployeeSystem.java file.

  1. Open the TestEmployeeSystem.java file located in the EmployeeSystemIoC/src/test/java/org/labex directory.

  2. Implement the test() method:

    • Create a ClassPathXmlApplicationContext object and load the applicationContext.xml file.
    • Retrieve the EmployeeServiceImpl bean from the Spring IoC container.
    • Retrieve the employee beans from the Spring IoC container and add them to the EmployeeServiceImpl using the addEmployee() method.
    • Print all the employee information by calling the getAllEmployees() method of the EmployeeServiceImpl bean.

Your TestEmployeeSystem.java file should look like this:

package org.labex;

import org.junit.Test;
import org.labex.pojo.Employee;
import org.labex.service.EmployeeServiceImpl;
import org.labex.service.IEmployeeService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestEmployeeSystem {

    @Test
    public void test(){
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        IEmployeeService employeeService = context.getBean(EmployeeServiceImpl.class);

        // Retrieve Employee beans from the context
        Employee emp1 = (Employee) context.getBean("emp1");
        employeeService.addEmployee(emp1);
        Employee emp2 = (Employee) context.getBean("emp2");
        employeeService.addEmployee(emp2);
        // Add more employee beans here

        // Print out employee information
        for (Employee e : employeeService.getAllEmployees()) {
            System.out.println(e.toString());
        }
    }
}

Now, you have completed the implementation of the Spring IoC Employee Management System. You can run the test() method to verify the functionality of the application.

  1. Run the test:
cd ~/project/EmployeeSystemIoC/
mvn test

The output should be similar to the following:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.labex.TestEmployeeSystem
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl addEmployee
INFO: Entering addEmployee() method
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl addEmployee
INFO: Entering addEmployee() method
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl addEmployee
INFO: Entering addEmployee() method
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl addEmployee
INFO: Entering addEmployee() method
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl addEmployee
INFO: Entering addEmployee() method
May 11, 2024 2:28:53 AM org.labex.service.EmployeeServiceImpl getAllEmployees
INFO: Entering getAllEmployees() method
Employees [employeeId=000001, employeeName=John Doe, employeeSex=Male, employeeBirthday=1993-11-06, employeeHiredate=2018-10-11, userId=1]
Employees [employeeId=000021, employeeName=Jane Smith, employeeSex=Female, employeeBirthday=1990-07-16, employeeHiredate=2019-10-21, userId=4]
Employees [employeeId=000022, employeeName=Alice Johnson, employeeSex=Female, employeeBirthday=1993-02-11, employeeHiredate=2019-12-27, userId=12]
Employees [employeeId=000035, employeeName=Bob Brown, employeeSex=Male, employeeBirthday=1991-06-23, employeeHiredate=2020-05-06, userId=19]
Employees [employeeId=000066, employeeName=Mary Taylor, employeeSex=Female, employeeBirthday=1997-12-21, employeeHiredate=2021-01-03, userId=20]
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.499 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.199 s
[INFO] Finished at: 2024-05-11T02:28:53Z
[INFO] ------------------------------------------------------------------------
âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Java Tutorials you may like