Spring Boot, Servlet Container, Web Server 관계 및 역할

Web, Servlet, Spring의 관계

카테고리

프로그래밍/소프트웨어 개발

서브카테고리

웹 개발

대상자

  • *Java 웹 개발자** (Spring/Spring Boot 프레임워크 사용자, 서블릿 아키텍처 이해 필요)
  • 난이도: 중급 (서블릿 컨테이너와 Spring 컨테이너 구분 이해 필요)*

핵심 요약

  • Web Server, Servlet Container, Spring Container는 서로 다른 역할을 수행하는 3개의 독립적인 컴포넌트
  • DispatcherServlet은 Spring MVC의 프론트 컨트롤러로, 요청 처리 흐름을 총괄 관리
  • Spring Boot는 내장 톰캣을 사용하여 Web Server + Servlet Container + Spring Container를 하나의 프로세스로 통합

섹션별 세부 요약

###1. 주요 구성 요소 역할 정의

  • Web Server (Nginx, Apache)

- HTTP 요청 수신 및 정적 리소스 제공

- 리버스 프록시 기능 지원

  • Servlet Container (톰캣, Jetty)

- HttpServlet 실행 및 서블릿 생명주기 관리

- DispatcherServlet 실행 환경 제공

  • Spring Container (ApplicationContext)

- Bean 생성, 의존성 주입, AOP, 트랜잭션 관리

###2. Spring Boot 환경 구성

  • 내장 톰캣 사용 시

- Web Server + Servlet Container + Spring Container가 단일 프로세스로 통합

  • 운영 환경 예시

```plaintext

[Nginx (Web Server)] → [Tomcat (Servlet Container)] → [Spring Container]

```

  • WebApplicationInitializer를 통해 DispatcherServlet 수동 등록 예시

```java

public class MyWebAppInitializer implements WebApplicationInitializer {

@Override

public void onStartup(ServletContext container) {

AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();

context.register(MyWebConfig.class);

DispatcherServlet dispatcherServlet = new DispatcherServlet(context);

container.addServlet("dispatcher", dispatcherServlet).addMapping("/");

}

}

```

###3. 요청 처리 흐름

  • HTTP 요청 처리 흐름

```plaintext

[Web Browser] → [Web Server] → [Servlet Container] → [DispatcherServlet] → [Spring Container]

```

  • DispatcherServlet의 핵심 역할

- HandlerMapping → HandlerAdapter → ViewResolver 흐름 지휘

  • Spring Container 초기화 과정
  1. 톰캣이 DispatcherServlet 초기화
  2. DispatcherServletWebApplicationContext 생성
  3. WebApplicationContext가 Spring Bean 관리

###4. 아키텍처 분리의 중요성

  • 모놀리식 → MSA 전환 시

- Web Server와 Servlet Container를 물리적으로 분리해야 확장성 확보

  • 디버깅 시 주의사항

- DispatcherServlet에서 Bean 찾지 못하면 Spring Container 초기화 실패 또는 연결 문제 가능성

결론

  • DispatcherServlet은 Spring MVC에서 요청 처리의 핵심이며, 서블릿 환경에서 실행되는 프레임워크임을 명확히 이해해야 디버깅 및 확장성 향상 가능
  • Spring Boot는 내장 톰캣을 통해 3개 컨테이너 통합을 자동화하나, 운영 환경에서는 분리된 아키텍처 설계가 권장됨
  • 다음 편에서는 HandlerMapping, HandlerAdapter, ViewResolver에 대한 심화 분석 예정