엔지니어로 가는 길

DispatcherServlet이 요청을 처리하는 과정 본문

프로그래밍/Spring

DispatcherServlet이 요청을 처리하는 과정

탐p슨 2020. 3. 10. 11:35
728x90

1. WebApplicationContext를 찾아 DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE에 담는다.

2. locale resolving이 필요한 경우 locale resolver를 찾아 다른 elements들이 locale 정보를 이용하여 요청을 처리할 수 있도록 한다. locale resolving이 필요하지 않다면 locale resolver는 필요없다.

3. theme resolving이 필요한 경우 theme resolver를 찾아 뷰와 같은 elements가 어떤 theme을 사용할지 결정할 수 있도록 한다. theme를 사용하지 않는다면 무시할 수 있다.

4. If you specify a multipart file resolver, the request is inspected for multiparts. If multiparts are found, the request is wrapped in a MultipartHttpServletRequest for further processing by other elements in the process.


5. 적절한 핸들러를 찾는다. 핸들러가 발견되면 모델 또는 렌더링을 준비하기 위해서 핸들러와 관련된 execution chain(preprocessors, postprocessors, and controllers)가 실행된다. 어노테이션이 붙은 컨트롤러에서는 뷰를 리턴하는 대신 HandlerAdpater 내에서 응답을 렌더링 할 수 있다.  (@RestController 에노테이션이 붙은 컨트롤러를 말하는 것 같다)

6. 모델이 리턴되면 뷰가 렌더링된다. 모델이 리턴되지 않으면(예를 들어 보안상의 문제로 preprocessor or postprocessor가 요청을 가로채면) 뷰는 렌더링되지 않는다.

 


 

HandlerExceptionResolver
HandlerExceptionResolver 빈은 WebApplicationContext에서 정의되며 요청을 처리하는 동안 발생한 예외를 처리하는데 사용된다. 이러한 exception resolver를 이용하여 예외를 처리하기 위한 로직을 커스터마이징 할 수 있다.

last-modification-date

DispatcherServlet은 Servlet API에 의해 구체화된 last-modification-date를 리턴할 수도 있다. 특정한 요청에 대한 last modification을 결정하는 과정은 직관적이다. DispatcherServlet이 적절한 핸들러 매핑을 찾은 뒤 핸들러가 LastModified라는 인터페이스를 구현했는지 확인한다. 만약 구현했다면 LastModified 인터페이스의 getLastModified 메소드의 값이 클라이언트에게 리턴된다.

 

Customize DispatcherServlet
Servlet initialization parameters(init-param elements)를 web.xml 속 Servlet 정의부에 추가함으로써 DispatcherServlet을 커스터마이징 할 수 있다.

Servlet initialization parameters

parameters

description

contextClass

ConfigurableWebApplicationContext를 구현한 클래스.

default: XmlWebApplicationContext 

contextConfigLocation

contextClass에서 명시한 context 인스턴스가 있는 경로

namespace

WebApplicationContext의 namespace.

default: [servlet-name]-servlet 

throwExceptionIfNoHandlerFound

요청에 대응되는 핸들러를 찾지 못했을 때 NoHandlerFoundException를 던질지 말지 설정.

default: false.

(false인 경우 DispatcherSerlvet은 예외를 던지지 않고 응답 상태로 404를 띄움. 만약 default servelet handling이 설정되어 있으면 unresolved 요청은 항상 default servlet로 가고, 404는 뜨지 않음.)

 

참고자료

https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-servlet-sequence

728x90
Comments