Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

개발 일기

springMVC설정 순서 정리! (feat. application-config.xml와 mvc-config.xml 비교) 본문

Spring

springMVC설정 순서 정리! (feat. application-config.xml와 mvc-config.xml 비교)

개발 일기 2021. 10. 6. 20:44

안녕하세요.

 

오늘은 Spring에서 가장 중요한,, 설정 파일들을 최초로 설정하는 순서?와 내용에 대해 정리한 부분을 학습하며

옮겨 적어보겠습니다.

 

추가로, bean 객체를 관리하는 두 개의 xml 파일 application-config.xml과 mvc-config.xml에 대해 간략 비교 한 내용을 적어 봅니다.

 

 

 

수업시간에 배운 정리해준 springMVC 설정 순서와 방식!!

 

A.spring-webmvc-4.3.25.RELEASE.jar라이브러리다운로드후 
  context/WEB-INF/lib 폴더에위치시킨다.(spring-webmvc-4.3.25.RELEASE.jar)

  ==>> pom.xml 설정 가능
  


B.
  - application-config.xml 파일생성(DataSource,Dao,Service 등 설정)

    <?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"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

    	<context:component-scan base-package="com.itwill.guest">
    		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    	</context:component-scan>

    	<context:property-placeholder location="classpath:db/jdbc.properties"/>
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    		<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>

    </beans>

 


  - mvc-config.xml 파일생성(HandlerMapping,Controller 등 설정)

    <?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:mvc="http://www.springframework.org/schema/mvc"
        xmlns:context="http://www.springframework.org/schema/context"
        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-4.3.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

        <mvc:annotation-driven/>
        <context:component-scan base-package="com.itwill.guest.controller"/>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/views/"/>
            <property name="suffix" value=".jsp"/>
        </bean>
    </beans>

 


C.  web.xml에 ContextLoaderListener설정(application-config.xml)

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring/application-config.xml</param-value>
	</context-param>

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

 


D. contoller설정파일위치파라메타등록, web.xml에 모든클라이언트요청(*.do)을 실행하도록  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>classpath:spring/mvc-config.xml</param-value>
  	</init-param>
  	<load-on-startup>1</load-on-startup>
  </servlet>

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



E. web.xml 에 인코딩 필터 클래스설정 

	<filter>
		<filter-name>encodingFilter</filter-name>
	<filter-class>
		org.springframework.web.filter.CharacterEncodingFilter
	</filter-class>
	<init-param>
		<param-name>encoding</param-name>
		<param-value>UTF-8</param-value>
	</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

  
F.Controller작성(Controller interface implement)
G.mvc-config.xml 파일에설정(mapping)

 

 


application-config.xml

- web.xml 에서 ContextLoaderListener에 의해 호출됨

- application context (root application context)를 생성함

- 주요 비즈니스, 메인프로세스, 모든 application context에 공유할 bean객체 를 담당

 

mvc-config.xml

- web.xml 에서 DispatcherServlet에 의해 호출됨

- web application context 를 생성함 (root application context의 하위 application context)

- 웹에 관련된 (웹 요청, 이동, 뷰리졸버 등등) bean객체를 담당함

출처: https://sourcestudy.tistory.com/459 [study]

 

 

 

강사님이 스프링은 초기 설정이 9할이라고 하셨는데,,

설정 방식이 많고 다양한 것 같습니다.. 수업 진도를 나가기 위해 정리해주신 기본대로 적어보았습니다.

 

감사합니다.

 

Comments