그전까진 우리가 직접 Model, View, Controller를 나눴다면 이번엔 스프링의 멋지게 MVC 나눠논 거를 봐보자~!

매우중요!!!!!!!!!!

아래 사진을 보면 우리가 지금까지 만든 구조와 스프링 MVC 구조가 똑같음을 알 수 있다!!

image.png

스프링의 DispatcherServlet이 FrontController임!

DispathcerServlet도 부모 클래스에서 HttpServlet을 상속받고 있음!

DispatcherServlet → FrameworkServlet → HttpServletBean → HttpServlet 순서로~

스프링 부트는 내장톰캣 서버를 띄울때 DispatcherServlet을 frontcontroller로 띄어서 모든 경로(urlPatterns = “ / “ ) 에 대해서 이 DispathcerServlet을 건너가게함!!!!!

서블릿이 호출되면 HttpServlet이 제공하는 service()가 호출되는데, 그 자식인 FrameworkServlet이 service()를 오버라이드 해두었다. 그 오버라이드 된 service를 호출하며

DispatcherServlet.doDispatch() 가 실행된다!!

아래 코드는 DispatcherServlet의 수많은 코드중, 저 위의 handler를 뒤져보는 Dispatcher(FrontController)의 역할들을 하는 중요 로직이다!! 예외 처리 땜시 매우 기니 (한글)주석 단 부분만 보고 넘어가자!

// 보면 얘가 HttpServletRequest, Response로 클라의 요청 응답을 받고 있음!
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
		HttpServletRequest processedRequest = request;
		HandlerExecutionChain mappedHandler = null;
		boolean multipartRequestParsed = false;

		WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);

		try {
			ModelAndView mv = null;
			Exception dispatchException = null;

			try {
				processedRequest = checkMultipart(request);
				multipartRequestParsed = (processedRequest != request);

				// 핸들러 매핑한다!!(1. 핸들러 조회!)
				mappedHandler = getHandler(processedRequest);
				if (mappedHandler == null) { // 핸들러 없으면 404 리턴!!!
					noHandlerFound(processedRequest, response);
					return;
				}

				// 핸들러 찾으면? 핸들러 어댑터 찾으러 감!!(2. 핸들러를 처리할 수 있는 핸들러 어댑터 조회)
				// 똑같이 루프 돌며 찾음.  (HandlerAdaer도 support랑 handle 있음)
				HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());

				// Process last-modified header, if supported by the handler.
				String method = request.getMethod();
				boolean isGet = HttpMethod.GET.matches(method);
				if (isGet || HttpMethod.HEAD.matches(method)) {
					long lastModified = ha.getLastModified(request, mappedHandler.getHandler());
					if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {
						return;
					}
				}

				if (!mappedHandler.applyPreHandle(processedRequest, response)) {
					return;
				}

				// 핸들러 어댑터로 맞는 핸들러 호출!!! -> modle and View로 받아서
				mv = ha.handle(processedRequest, response, mappedHandler.getHandler());

				if (asyncManager.isConcurrentHandlingStarted()) {
					return;
				}
				// modle and View 적용!
				applyDefaultViewName(processedRequest, mv);
				mappedHandler.applyPostHandle(processedRequest, response, mv);
			}
			catch (Exception ex) {
				dispatchException = ex;
			}
			catch (Throwable err) {
				dispatchException = new ServletException("Handler dispatch failed: " + err, err);
			}
			// Render() 실시! -> 여기서 뷰리졸버도 실시 
			processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);
		}
		catch (Exception ex) {
			triggerAfterCompletion(processedRequest, response, mappedHandler, ex);
		}
		catch (Throwable err) {
			triggerAfterCompletion(processedRequest, response, mappedHandler,
					new ServletException("Handler processing failed: " + err, err));
		}
		finally {
			if (asyncManager.isConcurrentHandlingStarted()) {
				// Instead of postHandle and afterCompletion
				if (mappedHandler != null) {
					mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);
				}
				asyncManager.setMultipartRequestParsed(multipartRequestParsed);
			}
			else {
				// Clean up any resources used by a multipart request.
				if (multipartRequestParsed || asyncManager.isMultipartRequestParsed()) {
					cleanupMultipart(processedRequest);
				}
			}
		}
	}