Chapter 5-2

ChangWoo·2023년 8월 5일
post-thumbnail

Part 5. 스프링 MVC의 기본 구조

5.2 예제 프로젝트의 로딩 구조

  • 프로젝트가 정상적으로 실행되면, 서버의 구동 시 약간의 로그가 기록된다.
  • 이 로그를 이용해 어떤 과정을 통해 프로젝트가 실행되즌지 알 수 있다.
  • 프로젝트 구동 시 관여하는 XML은 web.xml, root-context.xml, servlet-context.xml 파일로 web.xml은 Tomcat 구동과 관련된 설정이고, 나머지 두 파일은 스프링과 관련된 설정이다.
  • 프로젝트의 구동은 web.xml에서 시작하며, 상단에는 가장 먼저 구동 되는 Context Listener가 등록되어 있다.
<!-- 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>
  • < context-param > 에는 root-context.xml의 경로가 설정되어 있고, < listener > 에는 스프링 MVC의 ContextLoaderListener가 등록되어 있다.
  • ContextLoaderListener는 해당 웹 애플리케이션 구동 시 같이 동작하므로 해당 프로젝트를 실행하면 다음과 같은 로그가 기록된다.
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Wed Aug 02 14:39:28 KST 2023]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/root-context.xml]
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 1254 ms
  • root-context.xml이 처리되면 파일에 있는 빈(Bean) 설정들이 동작하게 되며 아래의 그림과 같다.
  • root-context.xml에 정의된 객체(Bean)들은 스프링의 영역(context) 안에 생성되고, 객체들 간의 의존성이 처리된다.
  • root-context.xml이 처리된 후에는 스프링 MVC에서 사용하는 DispatcherServlet이라는 서블릿과 관련된 설정이 동작한다.
	<!-- 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>
  • org.springframework.web.servlet.DispathcerServlet 클래스는 스프링 MVC의 구조에서 가장 핵심적을 역할을 하는 클래스다.
  • 내부적으로 웹 관련 처리의 준비 작업을 진행하는데 사용하는 파일이 servlet-context.xml이다.
  • 프로젝트가 실행될 때 로그의 일부를 보게 되면,
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing WebApplicationContext for namespace 'appServlet-servlet': startup date [Wed Aug 02 14:39:29 KST 2023]; parent: Root WebApplicationContext
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]
INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/],methods=[GET]}" onto public java.lang.String org.zerock.controller.HomeController.home(java.util.Locale,org.springframework.ui.Model)
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'appServlet-servlet': startup date [Wed Aug 02 14:39:29 KST 2023]; parent: Root WebApplicationContext
INFO : org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: WebApplicationContext for namespace 'appServlet-servlet': startup date [Wed Aug 02 14:39:29 KST 2023]; parent: Root WebApplicationContext
INFO : org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/resources/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
INFO : org.springframework.web.servlet.DispatcherServlet - FrameworkServlet 'appServlet': initialization completed in 2184 ms

DispatcherServlet에서 XmlWebApplicationContext를 이용해 servlet-context.xml을 로딩하고 해석하기 시작한다.

  • 이 과정에서 등록된 객체들은 기존에 만들어진 객체들과 같이 연동하게 된다.
profile
한 걸음 한 걸음 나아가는 개발자

0개의 댓글