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
IEmployeeServiceinterface to handle employee-related operations - How to create the
EmployeeServiceImplclass to provide the implementation of theIEmployeeServiceinterface - 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
Implement the IEmployeeService Interface
In this step, you will learn how to implement the IEmployeeService interface. Follow the steps below to complete this step:
Open the
IEmployeeService.javafile located in theEmployeeSystemIoC/src/main/java/org/labex/servicedirectory.Declare two methods in the
IEmployeeServiceinterface:addEmployee: This method takes anEmployeeobject 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 aList. 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();
}
Implement the EmployeeServiceImpl Class
In this step, you will learn how to implement the EmployeeServiceImpl class, which is the implementation of the IEmployeeService interface.
Create a new file called
EmployeeServiceImpl.javain theEmployeeSystemIoC/src/main/java/org/labex/servicedirectory.Implement the
EmployeeServiceImplclass and make it implement theIEmployeeServiceinterface.Annotate the
EmployeeServiceImplclass with the@Serviceannotation.Declare a static
Listcollection globally and assign a collection object in the parameterless constructor.Implement the
addEmployee()andgetAllEmployees()methods:- In the
addEmployee()method, log the execution process using thelogger.info()method. - In the
getAllEmployees()method, log the execution process using thelogger.info()method.
- In the
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;
}
}
Configure the Spring IoC Container
In this step, you will learn how to configure the Spring IoC container to enable annotation-based package scanning.
Open the
applicationContext.xmlfile located in theEmployeeSystemIoC/src/main/resourcesdirectory.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"Add the
<context:component-scan>element to enable annotation-based package scanning for theorg.labexpackage.
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>
Implement the Test Method
In this step, you will learn how to implement the test() method in the TestEmployeeSystem.java file.
Open the
TestEmployeeSystem.javafile located in theEmployeeSystemIoC/src/test/java/org/labexdirectory.Implement the
test()method:- Create a
ClassPathXmlApplicationContextobject and load theapplicationContext.xmlfile. - Retrieve the
EmployeeServiceImplbean from the Spring IoC container. - Retrieve the employee beans from the Spring IoC container and add them to the
EmployeeServiceImplusing theaddEmployee()method. - Print all the employee information by calling the
getAllEmployees()method of theEmployeeServiceImplbean.
- Create a
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.
- Run the test:
cd ~/project/EmployeeSystemIoC/
mvn test
The output should be similar to the following:
y 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
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



