Anyframe Core와의 연계

Anyframe Web 기반의 웹 어플리케이션을 개발할 때 일반적으로 MVC의 Model 영역에 해당하는 비지니스 객체는 Anyframe Core 기반의 비지니스 계층을 적용하게 된다.
여기에서는 Spring Java framework (http://www.springframework.org/)을 확장한 Anyframe Core 과의 연계를 위해 제공되는 Anyframe Web의 기능 및 설정에 대해 알아본다.
Anyframe Core와의 연계를 위한 주요 확인 부분은 다음과 같다.

Anyframe Core 연계 관련 설정 및 기능
  • web.xml의 listener 요소로 org.springframework.web.context.ContextLoaderListener 등록
  • web.xml에 context-param 요소로 contextConfigLocation 등록
  • Action 클래스 작성 시 Spring Bean 형태의 비지니스 객체를 호출
ContextLoaderListener 설정
Servelt 2.3 이상에서는 컨텍스트(하나의 웹 어플리케이션) 라이프 사이클 관련 이벤트인 ServletContextEvent와 세션의 라이프 사이클 관련 이벤트인 HttpSessionEvent가 추가되었다. web.xml listener element 에 이러한 웹 어플리케이션의 이벤트에 응답하는 Context Event Listner를 등록하므로써 이 Listener 구현 클래스에서는 컨텍스트 초기화나 종료 시점에 무언가 유용한 작업 (ex. 어플리케이션의 초기화 파라메터 로드, 서비스 컨테이너 기동 ..)을 수행할 수 있게 된다.
org.springframework.web.context.ContextLoaderListener 클래스는 ServletContextListener 인터페이스를 구현하고 있으며 어플리케이션이 서블릿 컨테이너에 의해 처음으로 로드되고 스타트된 시점에 발생하는 startup 이벤트와 어플리케이션이 셧다운 되는 시점에 발생하는 shutdown 이벤트를 처리할 수 있는 다음의 두 메소드를 포함한다.
  • contextInitialized
  • contextDestroyed
contextInitialized 메소드는 root WebApplicationContext 를 초기화하게 되고 contextConfigLocation 으로 설정된 빈 속성 정의 파일에 따라 서비스 인스턴스의 생성, 관리가 시작된다.
contextDestroyed 메소드에서는 관련 자원을 release 하고 root WebApplicationContext를 close 한다.
Anyframe Core 연계를 위한 web.xml (ContextLoaderListener, contextConfigLocation) 설정은 다음과 같다.
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>
		/config/spring/applicationContext-*.xml
	</param-value>
</context-param>
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
웹 어플리케이션의 Context Event Listner 로 ContextLoaderListener 를 지정하고 있다. Bean 설정 파일들은 contextConfigLocation 으로 지정한 파일 명 패턴에 맞는 경우 참조가 가능하다.

Samples

Anyframe Core 기반의 비지니스 객체 참조 예
다음은 DefaultActionSupport을 상속한 UpdateUserAction 클래스에서 Anyframe Core 기반의 UserService의 비지니스 메소드 호출의 예이다.
public class UpdateUserAction extends DefaultActionSupport {
	...

	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ApplicationContext ctx = getWebApplicationContext();
		UserService userService = (UserService) ctx.getBean("userService");

		UserVO userVO = new UserVO();
		UserForm userForm = (UserForm) form;
		BeanUtils.copyProperties(userVO, userForm);
		
		userService.updateUser(userVO);

		return mapping.findForward("success_update");
	}
}
Anyframe Web에서는 내부적으로 Spring의 ActionSupport를 확장한 DefaultAction과 이를 확장한 템플릿 클래스인 DefautActionSupport 을 제공하므로 Struts - Spring 쉬운 연계가 가능하며 위의 예와 같이 어플리케이션 컨텍스트를 얻어 ctx.getBean(서비스명) 과 같이 비지니스 메소드를 호출한다.
이 때 해당 서비스명은 contextConfigLocation 로 web.xml 에서 가리키고 있는 applicationContext-*.xml 에 등록되어 있어야 한다.
위에서 참조하는 Anyframe Core 기반의 비지니스 객체 설정 파일
<bean id="userService"
	class="com.sds.emp.user.services.impl.UserServiceImpl">
	<property name="userDAO">
		<bean class="com.sds.emp.user.services.impl.UserDAO">
			<property name="propertiesService"
				ref="propertiesService" />
			<property name="queryService" ref="queryService" />
			<property name="hibernateService" ref="hibernateService" />	
		</bean>
	</property>
</bean>
Anyframe Core 기반의 서비스 작성 시 Configuration 파일 설정에 관련된 자세한 사항은 Anyframe Core > New Service > UserService Configuration - applicationContext-user.xml 을 참조한다.