본문 바로가기

SPRING/Configuration

스프링 MVC의 기본 구조 및 흐름

프로젝트 구동 시 관여하는 XML 파일에는 여러 가지가 있다.

 

web.xml

Tomcat 구동과 관련된 설정으로 프로젝트 구동은 여기서부터 시작한다.

 

<?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">

	<!-- contextConfigLocation : 루트 빈 설정 파일의 경로 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
            <!-- param-value 태그로 여러 개의 빈 설정 파일 추가 가능 -->
		<param-value>/WEB-INF/spring/root-context.xml</param-value>
	</context-param>
	
	<!-- ContextLoaderListener : 설정 파일이 로드되도록 해주며, 서블릿을 초기화시켜줌 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 서블릿과 관련된 설정 -->
	<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> <!-- 모든 요청을 DispatcherServlet이 받음  -->
	</servlet-mapping>

</web-app>

 

 

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>

 

 

servlet-context.xml

그 다음 동작하는 스프링과 관련된 설정 파일로

이곳에 등록된 객체들은 root-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에 대한 설정이 담김 -->
    
	<!-- Enables the Spring MVC @Controller programming model -->
	<annotation-driven />
    
	<!-- resource 아래에 js, css, img와 같은 파일을 넣음 -->
	<resources mapping="/resources/**" location="/resources/" />

	<!-- ViewResolver : 어떤 view를 통해서 처리하는 것이 좋을지 해석하는 역할 -->
	<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="org.zerock.controller" />
	
</beans:beans>

 

 

설정 파일에 대한 해석이 끝나면 본격적인 흐름이 시작된다.

사용자의 Request가 들어온다.

 

모든 요청을 처리하는 DispatcherServlet을 통해 들어온 요청은 어떤 요청인지를 판단하기 위해

 

HandlerMapping으로 요청에 해당하는 컨트롤러를 찾는다.

@RequestMapping / @GetMapping / @PostMapping 어노테이션을 기준으로 찾는다.

 

적절한 컨트롤러를 찾았으면 HandlerAdaptor를 이용해서 해당 컨트롤러를 동작시킨다.

 

해당하는 Controller는 실제 요청에 해당하는 로직을 수행한다.

로직을 모두 수행하고 결과를 반환할 때 이에 대한 처리는 ViewResolver를 이용한다.

 

ViewResolver는 컨트롤러가 반환한 결과를 어떤 View를 통해서 처리하는 것이 좋을지 해석한다.

경로를 합성하여 해당 View로 보내준다.

 

View는 실제로 응답 보내야 하는 데이터를 jsp 파일을 통해 생성하며 만들어진 응답은

또 다시 DispatcherServlet으로 전송한다.

 

 

 

* Front-Controller 패턴

모든 요청은 DispatcherServlet을 통하도록 설계되어 있는 방식