2013년 6월 24일 월요일

2013-06-24 Springframework Config

Springframework Config

  • Maven Project 생성
        - pom.xml을 통해 library추가
        - dispatcher-servlet(front servlet)생성 <WEB-INF/dispatcher-servlet.xml>
        - services.xml 생성 <src/main/resource/services.xml>

  • web.xml

        1) springframework reference documentation에서 web.xml 검색
           5.12.2 Support for web applications with AnnotationConfigWebApplicationContext
 
        <!-- ContextLoaderListener 설정 -->
          <context-param>
              <param-name>contextConfigLocation</param-name>
              <param-value>classpath:services.xml</param-value>
          </context-param>

         <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener
            </listener-class>
         </listener>

         <!-- DispatcherServlet 설정 -->
          <servlet>
              <servlet-name>dispatcher</servlet-name>
              <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
              <init-param>
                  <param-name>contextConfigLocation</param-name>
                  <param-value>/WEB-INF/dispatch-context.xml</param-value>
              </init-param>
          </servlet>

          <servlet-mapping>
              <servlet-name>dispatcher</servlet-name>
              <url-pattern>*.do</url-pattern>
          </servlet-mapping>

        2) Springframework의 ContextLoaderListener와 DispatcherServlet 사용 위해
            pom.xml에 library 추가
            ○ org.springframework.web.context.ContextLoaderListener
-->spring-context : 3.2.3.RELEASE 추가
  ○ org.springframework.web.servlet.DispatcherServlet
-->spring-webmvc : 3.2.3,RELEASE 추가

  • dispatcher-servlet.xml

  1) springframework reference documentation에서 
    17.3.1 Defining a controller with @Controller 검색

          <beans xmlns="http://www.springframework.org/schema/beans"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xmlns:context="http://www.springframework.org/schema/context"
	       xsi:schemaLocation="
       	                  http://www.springframework.org/schema/beans
                   http://www.springframework.org/schema/beans/spring-beans.xsd
       	           http://www.springframework.org/schema/context
                                 http://www.springframework.org/schema/context/spring-context.xsd
            ">

             <!-- scan 경로 -->
	             <context:component-scan base-package="bit.java39.controls" />

        2) springframework reference documentation에서 view resolver검색
              <bean id="jspViewResolver"
	        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	        <property name="viewClass"
	        	value="org.springframework.web.servlet.view.JstlView" />
	        <property name="prefix" value="/WEB-INF/view/" /> <!-- 접두사 -->
	        <property name="suffix" value=".jsp" /> <!-- 접미사 -->
              </bean>


  • services.xml

        1) scan 경로 설정
        <beans xmlns="http://www.springframework.org/schema/beans"
        	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
              xmlns:context="http://www.springframework.org/schema/context"
        	xsi:schemaLocation="
               	  http://www.springframework.org/schema/beans
                     http://www.springframework.org/schema/beans/spring-beans.xsd
       	           http://www.springframework.org/schema/context
                   http://www.springframework.org/schema/context/spring-context.xsd">
        	<context:component-scan base-package="bit.java39.dao">

        2) properties (mybatis.org)
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="locations" value="classpath:data-access.properties"/>
        </bean>

        3) sqlSessionFactory
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="typeAliasesPackage" value="bit.java39.vo"/>
		<property name="mapperLocations"
                                   value="classpath*:bit/java39/dao/*Mapper.xml" />
        </bean>

        4) spring data source
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="${jdbc.driverClassName}" />
		<property name="url" value="${jdbc.url}" />
		<property name="username" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
        </bean>

        5) transactionManager
        <bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
        </bean>

        6) library 추가
        org.mybatis.spring.SqlSessionFactoryBean
         --> mybatis-spring : 1.2.0
        org.apache.commons.dbcp.BasicDataSource
         --> commons-dbcp : 1.4
        org.springframework.jdbc.datasource.DataSourceTransactionManager
         --> spring-jdbc : 3.2.3.RELEASE


  • 그 외 필요 library
        1) ojdbc14 : 10.2.0.4.0 추가
         --> library 추가시 에러 --> repositories추가
        <repositories>
		<repository>
			<id>mesir-repo</id>
			<url>http://mesir.googlecode.com/svn/trunk/mavenrepo</url>
		</repository>
	</repositories>

        2) mybatis : 3.2.2
        3) javax.servlet-api : 3.1.0
        4) jstl : 1.2

댓글 1개: