Spring Integration
Anyframe Web은 Spring MVC를 기반으로 구성되어 있으므로 Spring 프레임워크의 다른 모듈과의 연계가 용이하다.
일반 웹 애플리케이션을 개발할 때 Business Layer의 Business Logic을 이용하여 요청을 처리하게 되는데
이 때 Business Layer를 연계하기 위한 방법은 다음과 같다.
Listener 등록과 Spring 설정 파일 목록 위치 정의
Spring MVC에서는 DispatcherServlet을 사용하여 WebApplicationContext를 로드하게 된다. 이때 Presentation Layer에서 사용할 Business Layer의 서비스 bean들을
ContextLoaderListener 등록 후 contextConfigLocation으로 Spring 설정 파일 위치를 지정해줌으로써 Presentation Layer에서 Business 서비스 bean들을 호출하여 사용할 수 있다.
다음은 설정 예인
web.xml
파일의 일부이다.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!-- Spring 설정 파일 위치 지정 -->
/config/spring/context-*.xml
</param-value>
</context-param>
<!--리스너 등록 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
Dependency Injection을 통한 Business Service 호출
위와 같이 Listener를 등록하고 Spring 설정 파일 위치를 지정해 주었으면 일반 서비스 호출과 같이
Dependency Injection을 사용하여 Business Service를 호출할 수 있다.
먼저 다음
user-servlet.xml
파일에서 처럼 해당 controller bean 정의 부분에서 사용할 서비스
dependency를 정의한다.
<bean name="/userForm.do"
class="anyframe.sample.springmvc.web.controller.extensions.UserController">
<property name="userService" ref="userService" />
<property name="formView" value="/jsp/user/userForm.jsp"/>
<property name="showNewForm" value="true"/>
<property name="sessionForm" value="true"/>
</bean>
dependency를 정의한 후에 컨트롤러 클래스에서 Dependency Injection을 통해 Business Service를 사용할 수 있다.
다음은 Setter Injection을 통해 Business Service를 호출한
UserController.java
파일의
일부이다.
public class UserController extends AnyframeFormController {
UserService userService = null;
// setter injection of userSerivce
public void setUserService(UserService userService) {
this.userService = userService;
}
// override onSubmit() method.
public ModelAndView getUser(HttpServletRequest request,
HttpServletResponse response) throws Exception {
UserVO userVO = new UserVO();
// data binding using command object
bind(request,userVO);
// call business service
userVO = userService.getUser(userVO);
...생략...
}
Spring IoC 컨테이너 Dependency Injection에 대한 자세한 사항은
Anyframe Core 매뉴얼 >> Spring IoC >>
Dependencies
를 참고한다.
Resources
다운로드
이클립스 프로젝트 형태의 샘플 웹 어플리케이션을 포함하고 있는 anyframe-springmvc-sample-extensions.zip 파일을 다운받은 후, 테스트 환경 설정
을 참조하여
위에서 제시한 예제 코드를 실행해 볼 수 있다.
| Name
|
Download
|
| anyframe-springmvc-sample-extensions.zip |
Download
|
참고자료