はじめに
このプロジェクトでは、Spring Framework を使用して社員管理システムの設定ファイルを分割および統合する方法を学びます。このプロジェクトの目的は、設定をモジュール化し、<import>タグを使用してシステムの保守性と共同作業性を向上させるメリットを示すことです。
🎯 タスク
このプロジェクトでは、以下を学びます。
- Spring 機能(SpringData、SpringAOP、および SpringJDBC)に基づいて、
applicationContext.xmlファイルを個別の XML ファイルに分割する方法。 - 社員情報を処理し、Spring AOP を有効にし、データソースと JdbcTemplate を構成するための各個別の XML ファイルの設定を実装する方法。
<import>タグを使用して、個別の設定ファイルをapplicationContext.xmlファイルに再統合する方法。- 統合された設定をテストして、システムが正常に動作していることを確認する方法。
🏆 成果
このプロジェクトを完了すると、以下ができるようになります。
- Spring ベースのアプリケーションの設定をモジュール化する。
<import>タグを使用して設定を管理および保守する。- 保守性と共同作業性を向上させるために、設定における関心事の分離の重要性を理解する。
- Spring アプリケーションでデータソース、JdbcTemplate、および Spring AOP を構成する。
springData.xml 設定の実装
このステップでは、社員情報を格納するための springData.xml 設定ファイルを実装します。
/home/labex/project/EmployeeSystem/src/main/resourcesディレクトリ内のspringData.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: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 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 設定の実装
このステップでは、Spring AOP を有効にし、AspectJ アスペクトをアノテートし、推奨クラスを宣言するための springAOP.xml 設定ファイルを実装します。
/home/labex/project/EmployeeSystem/src/main/resourcesディレクトリ内のspringAOP.xmlファイルを開きます。- AOP を有効にし、
EmployeeAdviceビーンを宣言するために、以下の設定を追加します。
<?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 設定の実装
このステップでは、データソースと JdbcTemplate を構成するための springJDBC.xml 設定ファイルを実装します。
/home/labex/project/EmployeeSystem/src/main/resourcesディレクトリ内のspringJDBC.xmlファイルを開きます。- データソースと JdbcTemplate のビーンを定義するために、以下の設定を追加します。
<?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 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 id="template" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg index="0" ref="datasource" />
</bean>
</beans>
設定ファイルの統合
このステップでは、<import> タグを使用して 3 つの設定ファイルを applicationContext.xml ファイルに統合します。
/home/labex/project/EmployeeSystem/src/main/resourcesディレクトリ内のapplicationContext.xmlファイルを開きます。- 3 つの 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
注:「パスワードを入力してください:」と促された場合は、エンターキーを押してください!
- 以下のコマンドを実行してテストを実行します。
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 でさらに多くの実験を行って練習してください。



