spring의 장점 중하나는 다양한 데이타베이스 모델에 대한 자체적인 api를 가지고 있다는 것이다. 하나의 프레임워크 만을 사용하면 확장성에 문제가 있기 때문에 필요에 따라 다양한 프레임워크를 사용한다.
■ spring library 다운 받기.
http://www.springsource.org/ 에서 다운로드로 이동 후 Spring Framework 2.5.6.SEC01 is the current production release (requires Java 1.4+) 아래 Download 를 클릭 한다. 페이지 이동 후 Download Now를 클릭한다.
페이지 이동 후에 More를 클릭하여 2.5.6 을 클릭하면 spring-framework-2.5.6-with-dependencies.zip 와 spring-framework-2.5.6-with-docs.zip 파일을 다운로드 한다. (개인 정보를 입력하는 부분이 나오면 대충 입력해도 된다.)
다운로드 받은 파일을 압축을 해제하고 spring-framework-2.5.6-with-dependencies\dist(여기가 spring의 핵심 부분) 으로 간다. dist 폴더에 보이는 spring library 파일을 프로젝트의 lib 폴더에 추가한다.
다시 spring-framework-2.5.6-with-dependencies\dist\modules 폴더로 이동해서 spring-aop, spring-beans, spring-core, spring-jdbc, spring-orm, spring-tx, spring-web, spring-webmvc, spring-webmvc-struts 라이브러리 파일을 추가한다.
마지막으로 spring-framework-2.5.6-with-dependencies\lib\log4j 폴더에서 log4j-1.2.15 라이브러리를 추가한다.
그리고 Struts2 library에서 struts-2.0.14\lib 폴더에 있는 struts2-spring-plugin-2.0.14 라이브러리를 추가 한다.(Strust1에서는 Spring으로 가는 플러그인이 없다)
■ applicationContext.xml 을 만들어서 작업을 진행하는 동안 계속 추가해 난간다.
작업 내용이 추가된 applicationContext.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 -->
<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 -->
<bean id="sqlMapClient" <!-- 아이디는 임으로 지어낸다 -->
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"
p:dataSource-ref="dataSource" <!-- dataSource 참조 -->
p:configLocation="classpath:SqlMapConfig.xml" /> <!-- default 경로는 scr 이고, src 하위 아무 디렉토리나 참조하기 위해서는 classpath를 써주는게 좋다 --<
<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는 property다.-->
<!-- <property name="template" ref="template"/> 이렇게 써도 된다. 같은 방법 bean id="template"가 p:template-ref="template"의 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"/>
</beans>
※ DB와 연동하기 위해 여기서는 SqlMapClientTemplate 을 사용하는데 bean 주입을 이용한다. iBatis에서 SqlMapConfig.xml 트랜잭션을 정의한 부분이 applicationContext.xml으로 온다.
SqlMapConfig.xml을 참조하고 있는 bean이 필요한데, 이런 객체들을 컨테이너가 가지고 있다가 서비스를 한다.
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"
p:dataSource-ref="dataSource"
p:configLocation="classpath:SqlMapConfig.xml" />
이 부분에서 SqlMapConfig.xml 을 id가 참조하도록 지시한다. 이게 바로 bean 주입 방식이다. 개발자가 객체를 만들지 않고 bena에서 id만 정의 해주면 컨테이너가 이 객체를 다 가지고 있다. 그리고 컨테이너에 의해서 객체 서비스가 이루어 진다.
그리고(예를 들어) DAOImpl Class 에서
/*setter injection*/
private SqlMapClientTemplate template; //이게 bean 주입 방식 으로, applicationContext.xml(빈설정문서 프로퍼티가 동일해야 된다)를 로딩해서 이 template 객체를 만들어 준다.
public void setTemplate(SqlMapClientTemplate template){
this.template = template;
}
이런 식으로 사용한다. 이렇게 다른 곳에서 객체가 필요할 때도 이런 방법으로 만들어 낸다.
■ struts.properties
# struts2 + spring Factory
struts.objectFactory=org.apache.struts2.spring.StrutsSpringObjectFactory
//applicationContext.xml을 가지고 있는게 factory다. 여기서 스트럿츠가 스프링으로 간다는 것을 알려주고 연결한다. objectFactory를 설정해 주면 applicationContext를 이 factory가 갖게 된다. DI를 할 때, 인위적으로 applicationContext를 로드해서 리소스를 읽어 id를 참조해서 만들지 않는다.
struts.objectFactory.spring.autoWire=type // applicationContext.xml에 들어있는 각각의 bean 들을 설정하는데 그 bean은 autoWire 라는 뜻으로 우리가 설정하는 타입데로 알아서 처리하라는 뜻.
(이 때 factory를 적용하기 위해서는 struts가 spring으로 간다는 것을 알려주어야 하는데 이때 필요한 library가 struts2-spring-plugin-2.0.14 이다.)
이 두줄을 struts.properties 에 추가한다.
■ web.xml
<!-- spring scope (context parameter) -->
<!-- applicationContext.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>
<!-- spring scope ended -->
이 부분 까지 Strut2 + Spring + iBatis 연결 설정 부분에 대한 간략한 설명이다.
'FrameWork > Struts2' 카테고리의 다른 글
Struts2 Interceptor(인터셉터) 3. (0) | 2009.08.15 |
---|---|
Struts2에서 Action에 있는 데이타로 JSP에서 보여주기. (0) | 2009.07.09 |
Struts2 Interceptor(인터셉터) 2. (0) | 2009.07.09 |
Struts2에서 freemarker-2.3.8.jar와 ognl-2.6.11.jar 에 대한 내용. (0) | 2009.07.08 |
Struts2에서 Tiles Plugin 사용하기. (1) | 2009.07.08 |
Struts2 Interceptors(인터셉터) 1. (0) | 2009.07.07 |
Struts2 에서 upload & download 하기 3. (0) | 2009.07.07 |
Struts2 에서 upload & download 하기 2. (0) | 2009.07.07 |
Struts2 에서 upload & download 하기 1. (0) | 2009.07.06 |
Struts2 에서 session 처리하는 방법(로그인, 로그아웃 해보기). (0) | 2009.07.05 |