Spring MVC: Servlet Container, Spring Container

yshjft·2023년 12월 25일
0

Spring, JPA

목록 보기
16/16

Web Server, Web Application Server, Servlet

Web Server(WS)

  • 정적 리소스 제공
  • Apache가 WS에 속한다.

Web Application Server(WAS)

  • Web Server + 동적 리소스 지원 기능
  • DB 조회, 비즈니스 로직 수행 등을 담당
  • Tomcat이 WAS에 속한다.

Servlet Container

  • Web Container
  • Servlet 실행 및 관리
  • Spring에서 Tomcat이 Servlet Container의 역할을 수행
    • 물론 Tomcat이 다른 종류의 WAS로 대체될 수도 있다.

Servlet

  • 웹 서버로부터 요청을 받아서 처리하고 서버에 다시 응답을 하는 하나의 자바 프로그램
  • DispatcherServlet
    • 프레임워크의 중심에 있는 DispatcherServlet이 톰캣의 Servlet Container에 배포되어 돌아가는 Servlet중 하나

    • 하나의 웹어플리케이션에 DispatcherServlet이 여러개 존재할 수 있다(물론 많이 사용하는 방식은 아닌 것으로 보인다.). 이러한 경우 Spring Container(WebApplicationContext)도 DispatcherServlet 별로 존재하게 된다.

      In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, …. by Spring Doc

ServletContext

  • ServletServlet Container와 통신하기 위해서 사용되어지는 메서드들을 가지고 있는 클래스
  • ServletContext를 통해 Servlet 간 자원을 공유할 수 있다.
  • 하나의 Web Application 내에 하나의 ServletContext가 존재한다.

Spring Container, WebApplicationContext

Spring Container

  • Bean을 관리하는 컨테이너
  • IOC Container, Bean Factory, ApplicationContext

WebApplicationContext

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    /WEB-INF/conf/applicationContext.xml
  </param-value>
</context-param>

<listener>
  <listener-class>
    org.springframework.web.context.ContextLoaderListener
  </listener-class>
</listener>


<servlet> 
  <servlet-name>dispatcherServlet</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
    </param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>

<!-- /로 들어오는 요청은 dispatcherServlet에서 처리 -->
<servlet-mapping>  
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

Root WebApplicationContext

  • ApplicationContext, root-context
  • ContextLoaderListener에 의해 생성
  • 컨테이너 구동 시 생성된다.
  • 서로 다른 Servlet에서 공통적으로 사용하는 Bean을 관리한다.
  • Service, Repository 등 웹 환경에서 독립적인 Bean들을 관리한다.
  • WebApplicationContext에서 관리되는 Bean을 사용할 수 없다.

WebApplicationContext

  • servlet-context
  • DispatcherServlet에 의해 생성
  • Dispatcher Servlet이 init하는 시점에 생성된다.
    • load-on-startup옵션이 있다면 서블릿 컨텍스트를 초기화하는 시점에 미리 생성된다.
  • DispatcherServlet은 자신만의 WebApplicationContext를 가진다.
  • Controller 등 웹 관련 Bean들을 관리
  • Root WebApplicationContext에서 관리되는 Bean을 사용할 수 있다.

Bean 탐색 우선 순위

  • Bean 탐색 순서: WebApplicationContext → Root WebApplicationContext
  • Bean이 겹치는 경우: WebApplicationContext 우선

주제 관련하여 이전에 정리한 글

참고한 내용

profile
꾸준히 나아가자 🐢

0개의 댓글