DispatcherServlet 초기화 및 요청 처리

tokkaiiii·2025년 4월 25일

spring-mvc

목록 보기
4/27

(1) init

HttpServlet 클래스의 init이 호출되는데 여기서도 super init을 호출한다

 public void init(ServletConfig config) throws ServletException {
    super.init(config);
    this.cachedUseLegacyDoHead = Boolean.parseBoolean(config.getInitParameter("jakarta.servlet.http.legacyDoHead"));
  }

super init 은 GenericServlet의 init 이다

  public void init(ServletConfig config) throws ServletException {
    this.config = config;
    this.init();
  }

다시 하위 클래스 init 호출
HttpServletBean 의 init 호출
여기서 this.initServletBean(); 메소드 호출

FrameworkServlet 이다

 protected final void initServletBean() throws ServletException {
   ServletContext var10000 = this.getServletContext();
   String var10001 = this.getClass().getSimpleName();
   var10000.log("Initializing Spring " + var10001 + " '" + this.getServletName() + "'");
   if (this.logger.isInfoEnabled()) {
     this.logger.info("Initializing Servlet '" + this.getServletName() + "'");
   }

   long startTime = System.currentTimeMillis();

   try {
     this.webApplicationContext = this.initWebApplicationContext();
     this.initFrameworkServlet();
   } catch (RuntimeException | ServletException ex) {
     this.logger.error("Context initialization failed", ex);
     throw ex;
   }

   if (this.logger.isDebugEnabled()) {
     String value = this.enableLoggingRequestDetails ? "shown which may lead to unsafe logging of potentially sensitive data" : "masked to prevent unsafe logging of potentially sensitive data";
     this.logger.debug("enableLoggingRequestDetails='" + this.enableLoggingRequestDetails + "': request parameters and headers will be " + value);
   }

   if (this.logger.isInfoEnabled()) {
     this.logger.info("Completed initialization in " + (System.currentTimeMillis() - startTime) + " ms");
   }

 }

this.webApplicationContext = this.initWebApplicationContext(); 이 로직이 있는데
webApplicationContext 를 초기화하고 있다

 protected WebApplicationContext initWebApplicationContext() {
    WebApplicationContext rootContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    WebApplicationContext wac = null;
    if (this.webApplicationContext != null) {
      wac = this.webApplicationContext;
      if (wac instanceof ConfigurableWebApplicationContext) {
        ConfigurableWebApplicationContext cwac = (ConfigurableWebApplicationContext)wac;
        if (!cwac.isActive()) {
          if (cwac.getParent() == null) {
            cwac.setParent(rootContext);
          }

          this.configureAndRefreshWebApplicationContext(cwac);
        }
      }
    }

    if (wac == null) {
      wac = this.findWebApplicationContext();
    }

    if (wac == null) {
      wac = this.createWebApplicationContext(rootContext);
    }

    if (!this.refreshEventReceived) {
      synchronized(this.onRefreshMonitor) {
        this.onRefresh(wac);
      }
    }

    if (this.publishContext) {
      String attrName = this.getServletContextAttributeName();
      this.getServletContext().setAttribute(attrName, wac);
    }

    return wac;
  }

이렇게 로직을 따라가다보면 onRefresh 메소드로 dispatcherServlet 을 호출한다

(1) - 1 DispatcherServlet

protected void onRefresh(ApplicationContext context) {
    this.initStrategies(context);
  }

전략 초기화 작업 실행

  protected void initStrategies(ApplicationContext context) {
    this.initMultipartResolver(context);
    this.initLocaleResolver(context);
    this.initThemeResolver(context);
    this.initHandlerMappings(context);
    this.initHandlerAdapters(context);
    this.initHandlerExceptionResolvers(context);
    this.initRequestToViewNameTranslator(context);
    this.initViewResolvers(context);
    this.initFlashMapManager(context);
  }

(2) service

init 에서 작업을 마치면 service 실행
HttpServlet service

예시는 Get 방식으로

protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String method = req.getMethod();
    if (method.equals("GET")) {
      long lastModified = this.getLastModified(req);
      if (lastModified == -1L) {
        this.doGet(req, resp);

doGet 호출
FrameworkServlet

 protected final void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    this.processRequest(request, response);
  }

일련의 작업을 마치고

 this.doService(request, response);

DispatcherServlet의 doService 호출
그리고 여기서도 일련의 작업을 실행하고 doDispatch 를 실행한다

profile
풀스택 자바 개발자입니다

0개의 댓글