ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • bean config 설정 분리
    Java/Spring 2018. 8. 24. 16:05
    반응형

    applicationContext에 Data가 늘어날수록 파일이 무거워지기 때문에 Bean Config 설정을 분리한다.


    Bean들을 한 곳에 정의하고 Listener를 통해 모두 Loading한다.


    web.xml에서 ContextLoaderListener를 추가한다.


    <!-- needed for ContextLoaderListener -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring/*Context.xml</param-value> <!-- /WEB-INF/spring/아래 있는 Context.xml파일은 다 가져온다. -->
    </context-param>

    <!-- Bootstraps the root web application context before servlet initialization -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <param-value>/WEB-INF/spring/*Context.xml</param-value> <!-- /WEB-INF/spring/아래 있는 Context.xml파일은 다 가져온다. -->


    이미 Load된 Bean Container에 적재 된 Bean은 제외한다.


    applicationContext.xml에서는 URL관련 설정을 작성한다. ( HandlerMapping, HandlerAdapter, Controller, ViewResolver 등 )


    <?xml version="1.0" encoding="UTF-8"?>
    <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"
        xmlns:mvc="http://www.springframework.org/schema/mvc"
        xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
            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-4.3.xsd">

        <!-- package이하에 있는 동일한 Interface로 등록된 모든 클래스를 찾아 자동으로 주입 -->
        <context:component-scan base-package="com.ktds" />
        <mvc:annotation-driven />
        
        <mvc:resources location="/WEB-INF/static/js/" mapping="/js/**" />
        <mvc:resources location="/WEB-INF/static/css/" mapping="/css/**" />
        <mvc:resources location="/WEB-INF/static/img/" mapping="/img/**" />
        
        <mvc:interceptors>
            <mvc:interceptor>
                <mvc:mapping path="/board/**/"/> <!-- /board 아래 모든 url -->
                <bean id="sessionInterceptor"
                     class="com.ktds.common.SessionInterceptor">
                </bean>
            </mvc:interceptor>
        </mvc:interceptors>
        
        <bean id="viewResolver"
             class="org.springframework.web.servlet.view.InternalResourceViewResolver">
             <property name="prefix" value="/WEB-INF/view/"></property>
             <property name="suffix" value=".jsp" ></property>
        </bean>
        
        <!-- settings.properties 파일의 내용을 Bean Container에 적재시키기 -->
        <context:property-placeholder location="/WEB-INF/spring/settings.properties" />

        <!-- FileUpload -->
        <bean id="multipartResolver"
             class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
            <property name="maxUploadSize" value="104857600" /> <!-- 100MB -->
            <property name="defaultEncoding" value="UTF-8" />   
    </bean>
    </beans>


    rootContext.xml 에서는 DataBase 연동 설정을 작성한다. - DataSource, Transaction 등


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        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.xsd">
        
        <!-- DBCP 설정 -->
        <!-- Connection 여러개 만들어서 모아두는 Pool -->
        <bean id="dataSource"
         class="org.apache.commons.dbcp.BasicDataSource">
         <property name="driverClassName" value="${jdbc.driver}" />
         <property name="url" value="${jdbc.url}" />
         <property name="username" value="${jdbc.id}" />
         <property name="password" value="${jdbc.password}" />
        </bean>
        
        <!-- Spring JDBC -->
        <!-- JDBCTemplate Bean 생성 -->
        <bean id="jdbc.Template"
         class="org.springframework.jdbc.core.JdbcTemplate">
         <property name="dataSource" ref="dataSource" />
        </bean>

    </beans>


    * Controller가 늘어날수록 applicationContext.xml파일이 무거워지기 때문에

    Controller.xml을 생성해서 사용할Controller.xml을 만들고 resource를 import시킨다.


    <Package>Context.xml에서는 주로 Package별 Content를 작성한다. -> Service, Dao 등


    boardContext.xml에 board에 사용할 Query문을 작성한다.


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        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.xsd">
        
        
        <!-- BoardDaoImpl이 사용할 Query 작성 -->
        <bean id="boardQueries"
         class="java.util.ArrayList">
         <constructor-arg name="c">
           <list>
               <!-- 0번 Index, Insert -->
               <value>
                        INSERT INTO SPRING.BOARD (
                                         ID
                                         , SUBJECT
                                         , CONTENT
                                         , EMAIL
                                         , CRT_DT
                                         , MDFY_DT
                                         , FILE_NAME
                                         , ORIGIN_FILE_NAME
                                         )
                        VALUES (
                                     BOARD_ID_SEQ.NEXTVAL
                                         , ?
                                         , ?
                                         , ?
                                         , SYSDATE
                                         , SYSDATE
                                         , ?
                                         , ?
                         )
               </value>
               <!-- 1번 Index, Select -->
               <value>
                   SELECT  B.ID
                                , B.SUBJECT
                                , B.CONTENT
                                , B.EMAIL B_EMAIL
                                , B.CRT_DT
                                , B.MDFY_DT
                                , B.FILE_NAME
                                , B.ORIGIN_FILE_NAME
                                , M.EMAIL M_EMAIL
                                , M.NAME
                                , M.POINT
                        FROM    BOARD B
                                , MEMBERS M
                        WHERE   B.EMAIL = M.EMAIL
                        AND     ID = ?
               </value>
               <!-- 2번 Index, Delete -->
               <value>
                   DELETE
                   FROM    BOARD
                   WHERE   ID = ?
               </value>
               <!-- 3번 Index, Select -->
               <value>
                   SELECT  B.ID
                                , B.SUBJECT
                                , B.CONTENT
                                , B.EMAIL B_EMAIL
                                , B.CRT_DT
                                , B.MDFY_DT
                                , B.FILE_NAME
                                , B.ORIGIN_FILE_NAME
                                , M.EMAIL M_EMAIL
                                , M.NAME
                                , M.POINT
                        FROM    BOARD B
                                , MEMBERS M
                        WHERE   B.EMAIL = M.EMAIL
                        ORDER   BY ID DESC
               </value>
           </list>
         </constructor-arg>
        </bean>

    </beans>



    마찬가지로 memberContext.xml bean파일을 만들어서 사용할 Query문을 작성한다.


    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        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.xsd">
        
        <!-- MemberDaoImpl이 사용할 Query 작성 -->
        <bean id="memberQueries"
             class="java.util.ArrayList">
             <constructor-arg name="c">
               <list>
                   <!-- 0번 Index, Insert Member -->
               <value>
                   INSERT INTO SPRING.MEMBERS (
                                  EMAIL
                                  , NAME
                                  , PASSWORD
                             )
                        VALUES (
                                     ?
                                     , ?
                                     , ?
                             )
               </value>
               <!-- 1번 Index, Select -->
               <value>
                   SELECT  EMAIL
                           , NAME
                           , PASSWORD
                           , POINT
                        FROM    MEMBERS
                        WHERE   EMAIL = ?
                        AND     PASSWORD = ?
               </value>
               <!-- 2번 Index, Delete -->
               <value>
                   DELETE
                  FROM    MEMBERS
                  WHERE   EMAIL = ?
               </value>
               <!-- 3번 Index, Select -->
               <value>
                   SELECT  EMAIL
                           , NAME
                           , PASSWORD
                                , POINT
                        FROM    MEMBERS
               </value>
               <!-- 4번 Index, Update -->
               <value>
                   UPDATE  MEMBERS
                   SET     POINT = POINT + ?
                   WHERE   EMAIL = ?
               </value>
               </list>
             </constructor-arg>
        </bean>

    </beans>



    반응형

    'Java > Spring' 카테고리의 다른 글

    Static Resources  (0) 2018.08.24
    Interceptor  (0) 2018.08.24
    Application 예외 처리  (0) 2018.08.24
    File - Upload / Download  (0) 2018.08.23
    Session  (0) 2018.08.23

    댓글

Designed by Tistory.