-
web.xml 과 servlet-context.xml 과 root-context.xmlWeb Programming/Spring4.0 2020. 9. 9. 16:08반응형

Spring MVC Project를 만들었을 때 기본으로 추가되는 xml의 목록은 다음과 같다.
- pom.xml
- web.xml
- servlet-context.xml
- root-context.xml
이 중 pom.xml은 Maven Project 생성 시 모듈을 다운받기 위한 xml으로 알고있다.
나머지 3개의 xml은 Spring에서 사용되는 xml인데 그 구조를 파악하는 것이 쉽지 않다.
이번 글에서는 이 xml의 용도를 알아보기로 하자.
(※ 각 코드는 개개인의 설정에 따라 얼마든지 달라질 수 있다.)
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>처음 알아볼 xml은 web.xml이다. 이 파일은 Tomcat과 같은 WAS(Web Application Server)가 최초로 구동될 때, 각종 설정을 정의해주는 파일이다. 쉽게 말해 root-context.xml과 servlet-context.xml 같은 설정 파일을 어디서 가져올 것인지를 설정해주는 설정을 위한 설정 파일이라고 할 수 있다.
servlet-context.xml
<?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <beans:property name="prefix" value="/WEB-INF/views/" /> <beans:property name="suffix" value=".jsp" /> </beans:bean> <context:component-scan base-package="com.taek.testing" /> </beans:beans>두번째로 알아볼 xml은 servlet-context.xml이다. 이 파일은 MVC Model의 View와 관련된 객체를 정의한다.
url과 관련된 Controller, @(Annotation), ViewResolver 등의 설정이 이 파일에 포함된다. Front-End의 설정 파일이라고 생각하면 될 것이다.
root-context.xml
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- Root Context: defines shared resources visible to all other web components --> </beans>마지막으로 알아볼 xml은 root-context.xml이다. 이 파일은 MVC Model의 Model과 관련된 객체를 정의한다.
데이터와 관련된 Service, DAO, Database 등의 설정이 이 파일에 포함된다. Back-End의 설정 파일이라고 생각하면 될 것이다.

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc 이 xml들을 통해 웹 애플리케이션으로 들어오는 모든 요청에 대한 핸들링이 가능해진다.
반응형'Web Programming > Spring4.0' 카테고리의 다른 글
Spring Framework Annotation 정리 #1 (0) 2020.09.21 어노테이션(Annotation) 이란? (0) 2020.09.18 MVC Model1 과 MVC Model2 (0) 2020.09.09 서블릿(Servlet) 이란? (0) 2020.09.09 프레임워크(FrameWork) 란? (0) 2020.09.08