Spring Integration
Anyframe Web의 Struts 기반에서 웹 어플리케이션을 개발할 때 일반적으로 MVC의
Model 영역에 해당하는 비즈니스 객체는 Spring Framework 기반의 Bean 형태로 개발될 것이다.
따라서, 프리젠테이션 로직을 수행하는 Action 클래스에서 비즈니스 로직을 수행하는 Spring Framework
기반의 서비스에 접근하기 위한 방법에 대해서 살펴보도록 하자.
Configuration
Spring Framework과 연계를 위해 다음과 같은 속성 정의가 필요하다.
- web.xml에 org.springframework.web.context.ContextLoaderListener를 listener로 등록
- web.xml에 context-param 요소로 contextConfigLocation 등록
ContextLoaderListener, ContextConfigLocation 정의
Servlet 2.3 이상에서는 웹 컨텍스트(하나의 웹 어플리케이션) 라이프 사이클 관련 이벤트인
ServletContextEvent와 Session의 라이프 사이클 관련 이벤트인 HttpSessionEvent가
추가되었다. 따라서 web.xml에 이러한 웹 어플리케이션의 이벤트에 응답하는 Context Event Listner를
등록해줌으로써 해당 Listener 구현 클래스에서 웹 컨텍스트 초기나 종료 시점에 무언가 유용한 작업
(ex. 어플리케이션의 초기 속성 로드, 서비스 컨테이너 기동 등..)을 수행할 수 있도록 해야 한다.
org.springframework.web.context.ContextLoaderListener 클래스는 ServletContextListener 인터페이스를
구현하고 있으며, 어플리케이션이 Servlet 컨테이너에 의해 처음으로 로드되는 시점에 발생하는
startup 이벤트와 어플리케이션이 종료되는 시점에 발생하는 shutdown 이벤트를 처리할 수 있도록
다음의 두 메소드를 포함한다.
- contextInitialized : root WebApplicationContext를 초기화하고
contextConfigLocation에 정의된 Bean 속성 정의 XML 파일을 기반으로 관련된 서비스
인스턴스를 생성한다.
- contextDestroyed : 관련 자원을 release하고 root WebApplicationContext를
close 한다.
다음은 Spring Framework 연계를 위한
web.xml (ContextLoaderListener, contextConfigLocation)
의
일부이다.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/config/spring/context-*.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Action
다음은 AbstractActionSupport를 상속받아 로그인을 시도하는 사용자에 대한 유효성을 검증하기
위해 프리젠테이션 로직을 처리하는 LoginAction클래스의 일부이다.
AbstractActionSupport에서 제공하는 getService 메소드를 호출하여 접근해야 할 비즈니스
서비스를 얻어내고 있음을 알 수 있다.
public class LoginAction extends AbstractActionSupport{
// 중략 ...
public ActionForward process(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception {
AuthenticationService authenticationService =
(AuthenticationService) getService("authenticationService");
UserForm userForm = (UserForm) form;
UserVO userVO = new UserVO();
BeanUtils.copyProperties(userVO, userForm);
Subject subject = authenticationService.authenticate(userVO);
HttpSession session = request.getSession();
session.setAttribute("subject", subject);
return (mapping.findForward("success"));
}
}
Spring Framework 기반의 서비스 개발시 Bean 속성 정의에 관련된 자세한 사항은
Anyframe Core >> New Service >> How to make a service의 UserService Configuration
을
참고한다.
Resources
다운로드
이클립스 프로젝트 형태의 샘플 웹 어플리케이션을 포함하고 있는 anyframe-struts-sample-extensions.zip 파일을 다운받은 후,
테스트 환경 설정
을 참조하여 위에서 제시한 예제 코드를 실행해 볼 수 있다.
| Name |
Download |
| anyframe-struts-sample-extensions.zip |
Download
|
참고자료