소개
이 프로젝트에서는 Spring Framework 를 사용하여 직원 관리 시스템의 설정 파일을 분할하고 통합하는 방법을 배우게 됩니다. 이 프로젝트는 설정의 모듈화 이점과 <import> 태그를 사용하여 시스템의 유지 관리성 및 협업을 개선하는 방법을 보여주는 것을 목표로 합니다.
🎯 과제
이 프로젝트에서 다음을 배우게 됩니다:
applicationContext.xml파일을 Spring 기능 (SpringData, SpringAOP, SpringJDBC) 을 기반으로 별도의 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파일을 엽니다.- 다음 설정을 추가하여 직원 정보 빈 (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">
<!-- 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>
springAOP.xml 설정 구현
이 단계에서는 Spring AOP 를 활성화하고, AspectJ aspect 를 주석 처리하며, 권장 클래스를 선언하기 위해 springAOP.xml 설정 파일을 구현합니다.
/home/labex/project/EmployeeSystem/src/main/resources디렉토리에서springAOP.xml파일을 엽니다.- AOP 를 활성화하고
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>
springJDBC.xml 설정 구현
이 단계에서는 데이터 소스 (data source) 와 JdbcTemplate 을 구성하기 위해 springJDBC.xml 설정 파일을 구현합니다.
/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">
<!-- 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>
설정 파일 통합
이 단계에서는 <import> 태그를 사용하여 세 개의 설정 파일을 applicationContext.xml 파일에 통합합니다.
/home/labex/project/EmployeeSystem/src/main/resources디렉토리에서applicationContext.xml파일을 엽니다.- 세 개의 XML 파일을 import 하기 위해 다음 설정을 추가합니다:
<?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>
설정 테스트
이 단계에서는 설정이 올바르게 작동하는지 확인하기 위해 테스트 클래스를 실행합니다.
- 터미널을 열고 다음 명령을 사용하여
/home/labex/project/EmployeeSystem디렉토리로 이동합니다:
cd ~/project/EmployeeSystem/
- 다음 명령을 사용하여 MySQL 서비스를 시작합니다:
sudo service mysql start
- 다음 명령을 사용하여
employeesystem이라는 새 데이터베이스를 생성합니다:
mysql -uroot -e "CREATE DATABASE employeesystem"
- 다음 명령을 사용하여
employeesystem.sql파일을employeesystem데이터베이스로 import 합니다:
mysql -uroot -p employeesystem < employeesystem.sql
참고: Enter password: 프롬프트가 표시되면 그냥 Enter 키를 누르세요!
- 다음 명령을 실행하여 테스트를 실행합니다:
mvn test
테스트가 성공적으로 실행되어야 하며, 다음과 같은 출력을 볼 수 있습니다:
-------------------------------------------------------
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] ------------------------------------------------------------------------
축하합니다! <import> 태그를 사용하여 applicationContext.xml 파일을 성공적으로 분할하고 설정을 통합했습니다.
요약
축하합니다! 이 프로젝트를 완료했습니다. LabEx 에서 더 많은 랩 (lab) 을 연습하여 기술을 향상시킬 수 있습니다.



