Employee Management File Integration

JavaJavaBeginner
Practice Now

Introduction

In this project, you will learn how to split and integrate the configuration files for an employee management system using Spring Framework. The project aims to demonstrate the benefits of modularizing the configuration and using the <import> tag to improve the maintainability and collaboration of the system.

ðŸŽŊ Tasks

In this project, you will learn:

  • How to split the applicationContext.xml file into separate XML files based on the Spring functionality (SpringData, SpringAOP, and SpringJDBC).
  • How to implement the configuration for each separate XML file to handle employee information, enable Spring AOP, and configure the data source and JdbcTemplate.
  • How to integrate the separate configuration files back into the applicationContext.xml file using the <import> tag.
  • How to test the integrated configuration to ensure the system is working correctly.

🏆 Achievements

After completing this project, you will be able to:

  • Modularize the configuration of a Spring-based application.
  • Utilize the <import> tag to manage and maintain the configuration.
  • Understand the importance of separating concerns in the configuration to improve maintainability and collaboration.
  • Configure the data source, JdbcTemplate, and Spring AOP in a Spring application.

Implementing the springData.xml Configuration

In this step, you will implement the springData.xml configuration file to store employee information.

  1. Open the springData.xml file in the /home/labex/project/EmployeeSystem/src/main/resources directory.
  2. Add the following configuration to define the employee information beans:
<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd">

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

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

    <bean id="emp3" class="org.labex.pojo.Employees"
        c:employeeId="000022" c:employeeName="Ada" c:employeeSex="female"
        c:employeeBirthday="1993-02-11" c:employeeHiredate="2019-12-27" c:userId="12"/>

    <bean id="emp4" class="org.labex.pojo.Employees"
        c:employeeId="000035" c:employeeName="Brown" c:employeeSex="male"
        c:employeeBirthday="1991-06-23" c:employeeHiredate="2020-05-06" c:userId="19"/>

    <bean id="emp5" class="org.labex.pojo.Employees"
        c:employeeId="000066" c:employeeName="Emma" c:employeeSex="sss"
        c:employeeBirthday="1997-12-21" c:employeeHiredate="2021-01-03" c:userId="20"/>

</beans>
âœĻ Check Solution and Practice

Implementing the springAOP.xml Configuration

In this step, you will implement the springAOP.xml configuration file to enable Spring AOP, annotate AspectJ aspects, and declare the recommended classes.

  1. Open the springAOP.xml file in the /home/labex/project/EmployeeSystem/src/main/resources directory.
  2. Add the following configuration to enable AOP and declare the EmployeeAdvice bean:
<?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:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- AOP Configurations -->
    <aop:aspectj-autoproxy />
    <bean id="employeeAdvice" class="org.labex.advice.EmployeeAdvice" />

</beans>
âœĻ Check Solution and Practice

Implementing the springJDBC.xml Configuration

In this step, you will implement the springJDBC.xml configuration file to configure the data source and JdbcTemplate.

  1. Open the springJDBC.xml file in the /home/labex/project/EmployeeSystem/src/main/resources directory.
  2. Add the following configuration to define the data source and JdbcTemplate bean:
<?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: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">

    <!-- Property Placeholder for Database Configuration -->
    <context:property-placeholder location="classpath:database.properties"/>

    <!-- DataSource Bean Configuration -->
    <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
          p:driverClassName="${jdbc.driver}" p:url="${jdbc.url}"
          p:username="${jdbc.username}" p:password="${jdbc.password}" />

    <!-- JdbcTemplate Bean Configuration -->
    <bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg index="0" ref="datasource" />
    </bean>

</beans>
âœĻ Check Solution and Practice

Integrating the Configuration Files

In this step, you will integrate the three configuration files into the applicationContext.xml file using the <import> tag.

  1. Open the applicationContext.xml file in the /home/labex/project/EmployeeSystem/src/main/resources directory.
  2. Add the following configuration to import the three XML files:
<?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: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">

    <!-- Package scanning -->
    <context:component-scan base-package="org.labex.dao"/>

    <!-- Importing external configuration files -->
    <import resource="classpath:springData.xml"/>
    <import resource="classpath:springAOP.xml"/>
    <import resource="classpath:springJDBC.xml"/>

</beans>
âœĻ Check Solution and Practice

Testing the Configuration

In this step, you will run the test class to ensure that the configuration is working correctly.

  1. Open a terminal and navigate to the /home/labex/project/EmployeeSystem directory using the following command:
cd ~/project/EmployeeSystem/
  1. Start the MySQL service using the following command:
sudo service mysql start
  1. Create a new database named employeesystem using the following command:
mysql -uroot -e "CREATE DATABASE employeesystem"
  1. Import the employeesystem.sql file into the employeesystem database using the following command:
mysql -uroot -p employeesystem < employeesystem.sql

Note: If you are prompted with Enter password:, just hit enter!

  1. Run the following command to execute the test:
mvn test

The test should run successfully, and you should see the following output:

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running org.labex.TestEmployeeSystem
//...Omit the test output...
Results :

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

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.645 s
[INFO] Finished at: 2024-01-05T16:27:39Z
[INFO] ------------------------------------------------------------------------

Congratulations! You have successfully split the applicationContext.xml file and integrated the configuration using the <import> tag.

âœĻ 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