介绍
在本项目中,你将学习如何使用 Spring 框架拆分和整合员工管理系统的配置文件。该项目旨在展示配置模块化以及使用<import>标签来提高系统的可维护性和协作性的好处。
🎯 任务
在本项目中,你将学习:
- 如何根据 Spring 功能(SpringData、SpringAOP 和 SpringJDBC)将
applicationContext.xml文件拆分为单独的 XML 文件。 - 如何为每个单独的 XML 文件实现配置,以处理员工信息、启用 Spring AOP 并配置数据源和 JdbcTemplate。
- 如何使用
<import>标签将单独的配置文件重新集成回applicationContext.xml文件。 - 如何测试集成后的配置以确保系统正常运行。
🏆 成果
完成本项目后,你将能够:
- 对基于 Spring 的应用程序的配置进行模块化。
- 使用
<import>标签管理和维护配置。 - 理解在配置中分离关注点对提高可维护性和协作性的重要性。
- 在 Spring 应用程序中配置数据源、JdbcTemplate 和 Spring AOP。
实现 springData.xml 配置
在这一步中,你将实现springData.xml配置文件来存储员工信息。
- 在
/home/labex/project/EmployeeSystem/src/main/resources目录下打开springData.xml文件。 - 添加以下配置来定义员工信息 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:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 员工信息bean -->
<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>
实现 springAOP.xml 配置
在这一步中,你将实现springAOP.xml配置文件,以启用 Spring AOP、注释 AspectJ 切面并声明推荐的类。
- 在
/home/labex/project/EmployeeSystem/src/main/resources目录下打开springAOP.xml文件。 - 添加以下配置以启用 AOP 并声明
EmployeeAdvicebean:
<?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配置 -->
<aop:aspectj-autoproxy />
<bean id="employeeAdvice" class="org.labex.advice.EmployeeAdvice" />
</beans>
实现 springJDBC.xml 配置
在这一步中,你将实现springJDBC.xml配置文件来配置数据源和 JdbcTemplate。
- 在
/home/labex/project/EmployeeSystem/src/main/resources目录下打开springJDBC.xml文件。 - 添加以下配置来定义数据源和 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">
<!-- 数据库配置的属性占位符 -->
<context:property-placeholder location="classpath:database.properties"/>
<!-- 数据源Bean配置 -->
<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配置 -->
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg index="0" ref="datasource" />
</bean>
</beans>
集成配置文件
在这一步中,你将使用<import>标签把三个配置文件整合到applicationContext.xml文件中。
- 在
/home/labex/project/EmployeeSystem/src/main/resources目录下打开applicationContext.xml文件。 - 添加以下配置以导入这三个 XML 文件:
<?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">
<!-- 包扫描 -->
<context:component-scan base-package="org.labex.dao"/>
<!-- 导入外部配置文件 -->
<import resource="classpath:springData.xml"/>
<import resource="classpath:springAOP.xml"/>
<import resource="classpath:springJDBC.xml"/>
</beans>
测试配置
在这一步中,你将运行测试类以确保配置正常工作。
- 打开一个终端,并使用以下命令导航到
/home/labex/project/EmployeeSystem目录:
cd ~/project/EmployeeSystem/
- 使用以下命令启动 MySQL 服务:
sudo service mysql start
- 使用以下命令创建一个名为
employeesystem的新数据库:
mysql -uroot -e "CREATE DATABASE employeesystem"
- 使用以下命令将
employeesystem.sql文件导入到employeesystem数据库中:
mysql -uroot -p employeesystem < employeesystem.sql
注意:如果系统提示Enter password:,直接回车即可!
- 运行以下命令执行测试:
mvn test
测试应该会成功运行,你应该会看到以下输出:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running org.labex.TestEmployeeSystem
//...省略测试输出...
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] ------------------------------------------------------------------------
恭喜!你已成功拆分applicationContext.xml文件并使用<import>标签整合了配置。
总结
恭喜!你已完成这个项目。你可以在 LabEx 中练习更多实验来提升你的技能。



