1. DispatcherServlet이란?
Spring MVC, as many other web frameworks, is designed around the front controller pattern where a central Servlet, the DispatcherServlet, provides a shared algorithm for request processing, while actual work is performed by configurable delegate components. This model is flexible and supports diverse workflows.
The DispatcherServlet, as any Servlet, needs to be declared and mapped according to the Servlet specification by using Java configuration or in web.xml. In turn, the DispatcherServlet uses Spring configuration to discover the delegate components it needs for request mapping, view resolution, exception handling, and more.
- Client의 Request에 대해 실제 처리하는 method를 호출해주거나 하는 front Controller의 역할을 해주는 Instance가 필요한데 Spring에서는 DispatcherServlet이 한다.
- 사용자의 요청마다 필요한 servlet을 web.xml에 등록하고 매핑하는 것은 수많은 요청이 있는 웹앱에서는 매우 번거로울 것이다. 그래서 생겨난 패턴이 front controller이다.
- 기존에는 아래와 같이 요청 url에 각각의 서블릿을 생성하고 controller에 요청을 보내주는 코드를 각각 작성해줬다.
- front controller에서는 하나의 서블릿이 요청에 맞는 컨트롤러에 요청을 전달한다.
- 스프링 MVC에서 DispatcherServlet은 클라이언트의 request를 컨트롤러에 전달할 뿐만 아니라, 스프링 Ioc 컨테이너와 통합하여 스프링의 모든 기능을 제공한다.
- DispatcherServlet은 request 처리를 공통되는 알고리즘을 이용하여 한다.
2. 스프링 DispatcherServlet 설정 방법
The DispatcherServlet, as any Servlet, needs to be declared and mapped according to the Servlet specification by using Java configuration or in web.xml. In turn, the DispatcherServlet uses Spring configuration to discover the delegate components it needs for request mapping, view resolution, exception handling, and more.
- DispatcherServlet도 실제 서블릿으로서 Servlet specification에 맞게 Java configuration 또는 web.xml에서 선언 및 mapping해줘야 한다. 아래는 WEB-INF 디렉토리 에 있는 web.xml 파일이며 DispatcherServlet이 처리할 url-mapping을 반드시 같이 작성해주어야 한다.
<!-- DispatcherServlet : 스프링에서 제공, 사용자의 요청을 받아주는 용도의 객체로 아래 경로를 읽어들여 IOC 컨테이너 생성
servlet-context.xml : DispatcherServlet 객체 생성 시 읽어들일 문서로 각 요청에 처리하는 컨트롤러를 지정하는
핸들러 매핑과 요청 처리 후 뷰를 지정하는 뷰 리졸버의 정보들을 저장하고 있는 xml 문서
-->
<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
/WEB-INF/spring/appServlet/aspect-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
3. Context Hierachy
For many applications, having a single WebApplicationContext is simple and suffices. It is also possible to have a context hierarchy where one root WebApplicationContext is shared across multiple DispatcherServlet (or other Servlet) instances, each with its own child WebApplicationContext configuration. See Additional Capabilities of the ApplicationContext for more on the context hierarchy feature.
The root WebApplicationContext typically contains infrastructure beans, such as data repositories and business services that need to be shared across multiple Servlet instances. Those beans are effectively inherited and can be overridden (that is, re-declared) in the Servlet-specific child WebApplicationContext, which typically contains beans local to the given Servlet. The following image shows this relationship:
- ContextLoaderListener -> WebApplicationContext 객체를 생성 [Root WebApplicationContext]
DispatcherServlet -> WebApplicationContext 객체를 생성 [Servlet WebApplicationContext] - DispatcherServlet이 생성하는 Servlet WebApplicationContext는 Root WebApplicationContext를 부모로 사용하는 자식 context이다. 자식 context인 Servlet WebApplicationContext root는 부모인 Root WebApplicationContext가 제공하는 빈을 사용할 수 있기 때문에 DispatcherServlet이 공통으로 필요로 하는 빈은 ContextLoaderListener를 이용하여 설정하는 것이다.
- DispatcherServlet은 root WebApplicationContext 를 공유한다.(DispatcherServlet 도 서블릿이므로 여러 개 생성이 가능하다.)
- Servlet WebApplicationContext : Controller, ViewResolver, HandlerMapping 등 웹과 관련된 스프링 빈(Beans)
Root WebApplicationContext : 모든 서블릿이 공유할 수 있는 Service, Repository 와 같은 스프링 빈(Beans)을 가진다.
참고 및 출처 :
1. 프론트 컨트롤러 https://galid1.tistory.com/525
2. Context Hierachy https://neloi.tistory.com/18
3. dispatcher servlet https://sabarada.tistory.com/16
https://midas123.tistory.com/222
4. 전체 https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-servlet
'spring' 카테고리의 다른 글
[빌드도구] maven & gradle 이란? (0) | 2022.10.11 |
---|---|
[spring] POJO (0) | 2022.01.09 |
[spring] Context란? (0) | 2021.10.20 |
[spring] annotation 이용한 DI (0) | 2021.10.20 |
[spring] 느슨한 결합력과 인터페이스(DI 이해에 도움) (0) | 2021.10.15 |