본문으로 바로가기

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

프로젝트가 실행할 때 나타나는 로그를 이용해서 어떤 과정을 통해서 프로젝트가 실행되는 지를 엿볼 수 있다. 프로젝트 구동 시 관여하는 XML은 web.xml,

root-context.xml, servlet-context.xml 파일임. 이 파일들중 web.cml은 Tomcat 구동과 관련된 설정이고,나머지 두 파일은 스프링과 관련된 설정임.

프로젝트의 구동은 web.xml에서 시작함.web.xml의 상단에는 가장 먼저 구동되는 Context Listener가 등록되어 있음.


web.xml일부


1
2
3
4
5
6
7
8
9
10
11
<!-- 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>
 
cs


<context-param>에는 root-context.xml의 경로가 설정되어 있고,<listener>에는  스프링MVC의 ContextLoaderListener가 등록되어 있는 것을 볼 수 있음.

ContextLoaderListener는 해당 웹 애플리케이션 구동 시 같이 동작하므로 해당 프로젝트를 실행하면 다음과 같이 가장 먼저 로그를 출력하면서 기록하는 것을 볼 수 있음.


1
2
3
4
5
6
7
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Mon Dec 17 17:10:25 KST 2018]; 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.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.
factory.support.DefaultListableBeanFactory@7c1e32c9: defining beans []; root of factory hierarchy
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 386 ms
12월 172018 5:10:25 오후 org.apache.catalina.core.ApplicationContext log
cs


root-context.xml이 처리되면 파일에 있는 빈(Bean) 설정들이 동작하게 됨.그림으로 표현하면  다음과 같이 표현됨.



root-context.xml에 정의된 객체(Bean)들은 스프링의 영역(context) 안에 생성되고,객체들 간의 의존성이 처리됨.

root-context.xml이 처리된 후에는 스프링MVC에서 사용하는 DispatcherServlet이라는 서블릿과 관련된 설정이 동작함.


web.xml 일부


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!-- 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>
 
cs

org.springframework.web.servlet.DispatcherServlet클래스는 스프링 MVC의 구조에서 가장 핵심적인 역할을 하는 클래스 .

내부적으로 웹 관련 처리의 준비작업을 진행하는데 이때 사용하는 파일이 servlet-context.xml임.

프로젝트가 실행될때 로그의 일부를 보면 아래 와 같음.

1
2
3
4
5
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 [Mon Dec 17 17:10:25 KST 2018]; 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.context.annotation.ClassPathBeanDefinitionScanner - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning
cs


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

이과정에서 등록된 객체(Bean)들을 기존에 만들어진 객체(Bean)들과 연동되게 됨.


▶스프링MVC의 기본 사상

스프링은 모델2에 대한 부분은 개발자들에게 보여주지 않고 ,개발자들은 자신이 필요한 부분만을 집중해서 개발할 수 있는 구조로 만들어져 있음.

웹 프로그래밍을 배워본 적이 있다면 가장 익숙한 단어들 중 하나는 Request/Response일 것이다.Servlet/JSP에서는 HttpServletRequest/HttpServletResponse라는 

타입의 객체를 이용해 브라우저에서 전송한 정보를 처리하는 방식임.

스프링MVC의 경우 이 위에 하난의 계층을 더한 형태가 됨.



스프링MVC를 이용하게 되면 개발자들은 직접적으로 HttpServletRequest/HttpServletResponse 등과 같이 Servlet/JSP의 API를 사용할 필요성이 현저하게 줄어듬.

스프링은 중간에 연결 역할을 하기 때문에 이러한 코드를 작성하지 않고도 원하는 기능을 구현할 수 있게됩니다.


개발자의 코드는 스프링MVC에서 동작하기 때문에 과거에는 스프링MVC의 특정한 클래스를 상속하거나 인터페이스를 구현하는 형태로 개발할 수 있었지만 ,스프링2.5버전 부터 등장한 어노테이션 방식으로 인해 최근 개발에는 어노테이션이나 XML등의 설정만으로 개발이 가능하게 되었음.