Spring Configuration
From JCFWiKi
Copyright © 2007 Daewoo Information Systems Co., Ltd. |
|
[그림1]Spring Framework with iBATIS Persistence Layer(참조:http://www.devx.com/Java/Article/31481/1763/page/2
목차 |
[편집] 서론
이 문서에서는 applicationContext.xml에 필수적으로 들어가는 tag와 configuration전략을 중점으로 설명하며 spring에 대한 구체적인 구조는 생략하기로 하곘다.
- [그림1]은 spring의 configuration이 persistance tier와 presentation tier을 연결해주는 고리가 되고 있음을 볼 수 있다.
- 아래 글에서는 각 클래스 사이의 의존 관계를 나타내는 spring configuration 및 각 tier별로 요구되는 bean 설정법을 기술하도록 하겠다
[편집] Basic
[편집] Spring Context configuration
- Web Application (web.xml)
<web-app> … <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/config/applicationContext*.xml </param-value> </context-param> <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>...
</web-app>
[편집] Spring DataSource Configuration
- applicationContext.xml
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list><value>classpath:/app.properties</value></list> </property> </bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" > <property name="driverClassName"><value>${driver}</value></property> <property name="url"><value>${url}</value></property> <property name="username"><value>${username}</value></property> <property name="password"><value>${password}</value></property> </bean>
- src/app.properties
- 프레임워크의 설정 파일들에서 사용하는 변수들에 대한 값을 설정
- 개발될 웹 어플리케이션에서 가변적인 부분에 변경을 집중적으로 관리하도록 지원
driver=oracle.jdbc.driver.OracleDriver url=jdbc:oracle:thin:@127.0.0.1:1521:okcode username=cube password=cube123
[편집] Spring iBatis Configuration
- applicationContext.xml
- 전체 application에 적용되는 xml 파일
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"><property name="configLocation"><value>classpath:/config/sqlmap-config.xml</value></property><property name="dataSource"><ref bean="dataSource" /></property></bean>
- applicationContext-article.xml
- Article 모듈에 적용되는 xml 파일
- applicationContext.xml을 참조함
<bean id="articleDao" class="jcf.showcase.article.dao.ArticleDao" ><property name="sqlMapClient" ref="sqlMapClient"/></bean><bean id="fileDAO" class="jcf.file.dao.FileDAOiBatis" >
<property name="sqlMapClient" ref="sqlMapClient"/></bean>
[편집] Spring Service Configuration
[편집] Dependency Injection
- 각 클래스 사이의 의존관계를 빈 설정 정보를 바탕으로 컨테이너가 자동적으로 연결해줌.
- Setter Injection
- 클래스 사이의 의존관계를 연결시키기 위해 setter 메소드를 이용
private UserDao userDao; public void setUserDao(UserDao dao) { this.userDao = dao; }
<bean id="userService" class="jcf.showcase.user.service.UserService" ><property name="userDao" ref="userDao"/></bean><bean id="userDao" class="jcf.showcase.user.dao.UserDao" ><property name="sqlMapClient" ref="sqlMapClient"/></bean>
