struts 2 - 스트러츠2 + spring 연동하기

Published on: 2009. 7. 12. 17:33 by louis.dev


우리나라에서는 struts 1 또는 spring 프레임웍을 사용하여 프로젝트를 구성하는 일이 많다고 한다.

물론 두 프레임웍은 뛰어난 프레임웍들이며 특히 스프링 같은 경우는 풀 스펙을 지원하는 프레임웍으로서 MVC모델을 기반으로 확장을 용의하게 하는 장점이 있다.

하지만 이같은 풀스펙의 스프링은 컨트롤러 부분이 약하다는 평가를 받고 있으며 실제 프로젝트 수행중에 스프링을 이용하면 스프링에서 지원하는 Controller를 사용해서 개발 하다 보면 불편한 점이 많다.

그래서 스트럿츠2는 Controller역할을 하고 spring은 iBatis와 연계하여 템플릿으로 DB에 접근하는 model을 사용하면 단일 프레임웍을 써서 개발하는 것보다 훨씬 수월하고 견고하게 개발을 할 수 있게 된다.


▶ Spring FrameWork Library 파일 추가

www.springsource.org 로 이동하여 FrameWork 라이브러리를 다운받는다.

- spring-framework-2.5.6-with-dependencies.zip   : 실제 SpringFrameWork을 사용할때 필요한 library파일이다.
- spring-framework-2.5.6-with-docs.zip  : SpringFrameWork의 Document 이다. 필요할때 참고하기 위해서 다운 받는다.   

다운받은 라이브러리 압축파일을 풀면 하위 폴더의 \dist 안에 있는 library파일을 모두복사하여 추가한다.(스프링의 핵심적인 라이브러리)

그리고 \dist\modules 로 이동하여 다음과 같은 라이브러리를 복사한다.
 - spring-aop.jar
 - spring-beans.jar
 - spring-core.jar
 - spring-jdbc.jar
 - spring-orm.jar
 - spring-tx.jar
 - spring-web.jar
 - spring-webmvc.jar
 - spring-webmvc-struts.jar

마지막으로 \lib\log4j 로 이동하여 log4j-1.2.15.jar를 추가한다.

struts2의 플러그인에는 struts2-spring-plugin-2.0.14.jar 를 추가 한다.

▶application-context.xml(spring과 iBatis 템플릿을 사용하기 위함)을 web.xml파일과 동일한 위치에 생성한다.

----------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.springframework.org/schema/beans  
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
      
 
  <!-- DataSource JDBC type -->
<!-- dataSource를 선언한 부분 --> 
<bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource"
        p:driverClassName="oracle.jdbc.driver.OracleDriver"
        p:url="jdbc:oracle:thin:@localhost:1521:XE"
        p:username="user01"
        p:password="user01" />
 
  <!-- iBatis SQL-Map config -->

<!--
    위에 선언한 dataSource를 정의해 주고 iBatis 설정이 들어간 SqlMapConfig 파일의 위치를 설정해 준다.
    SqlMapConfig.xml파일의 default는 src(classes)폴더이고 classpath를 써주게 되면 아무데나 놓아서 검색하여 찾아낸다..
-->

  <bean id="sqlMapClient"
        class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"
        p:dataSource-ref="dataSource"
        p:configLocation="classpath:SqlMapConfig.xml" />
  
<!-- ibatis의template를 사용하기 위한 bean 설정이다. -->
  <bean id="template"
        class="org.springframework.orm.ibatis.SqlMapClientTemplate"
        p:sqlMapClient-ref="sqlMapClient"/>     
                 
  <bean id="scheduleDao"
        class="com.myhome.schedule.model.ScheduleDAOImpl"
        p:template-ref="template" />
   <!--
           p:template-ref="template" 이것은
           <property name="template" ref="template"/>
           이런식으로 설정해도 된다.
    -->     
  <bean id="service"
        class="com.myhome.service.ScheduleServiceImpl"
        p:scheduleDao-ref="scheduleDao"/>
       
  <bean id="dto"
        class="com.myhome.schedule.model.ScheduleDTO"/>
  
  <bean id="scheduleModel"
        class="com.myhome.schedule.api.ScheduleModel"/>    
 
  <bean id="mc" class="com.myhome.schedule.api.MyCalendar"/>
       
</beans>

 ----------------------------------------------------------------------------------------------------

DB와 연동하기 위해 SqlMapClientTemplate 사용하는데 bean객체의 주입으로 데이터가 이동한다.

<bean id="scheduleDao"
        class="com.myhome.schedule.model.ScheduleDAOImpl"
        p:template-ref="template" />
이렇게 선언되어 있으면 위에서 선언한 class(ScheduleDAOImpl)에서는 setter injection으로 bean객체를 생성없이 사용할 수 있다.

즉 위와 같이 scheduleDao가 선언되어 있으면 이 bean을 사용할 ScheduleDAOImpl 에서는 다음과 같이 객체생성 없이 사용 할 수 있다는 말이다.

/*setter injection*/
 private SqlMapClientTemplate template;
 public void setTemplate(SqlMapClientTemplate template){
  this.template = template;
 }

이렇게 되면 바로 template.~~ 로 insert나 update의 method를 호출하여 사용할 수가 있다.
중요한 점은 위의 appication-context.xml파일에서 설정한 scheduleDao bean에서 선언한 ref의 이름과 실제 setter injection으로 받을 property이름이 같아야 한다는 점이다.

▶ struts.properties 파일에 spring과 연동하겠다고 선언해주어야 함

struts.objectFactory=org.apache.struts2.spring.StrutsSpringObjectFactory
//applicationContext.xml을 가지고 있는게 factory다.
여기서 스트럿츠가 스프링으로 간다는 것을 알려주고 연결한다. objectFactory를 설정해 주면 applicationContext를 이 factory가 갖게 된다. id를 할 때, 인위적으로 applicationContext를 로드해서 리소스를 읽어 id를 참조해서 만들지 않는다.

struts.objectFactory.spring.autoWire=type
// applicationContext.xml에 들어있는 각각의 bean 들을 설정하는데 그 bean은 autoWire 라는 뜻으로 우리가 설정하는 타입데로 알아서 처리하라는 뜻.

▶ web.xml에 설정
<!-- spring 설정파일인 appicationContext.xml의 위치를 선언한다.  -->

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
 
  <listener>
     <listener-class>
        org.springframework.web.context.ContextLoaderListener
     </listener-class>
  </listener>







'Framework > STRUTS 2' 카테고리의 다른 글

struts2 - struts 2의 Exception 처리  (2) 2009.08.09
struts 2 - JFree Chart plugin  (0) 2009.07.08
struts 2 - Tiles Plug in  (6) 2009.07.08
struts 2 - Interceptor  (3) 2009.07.06
struts 2 - properties 파일  (1) 2009.07.05