티스토리 뷰
0. Spring MVC - Servlet
:: Connection = 1 요청 : 1 스레드
:: 이때, Servlet 객체는 Singleton
:: 즉, Servlet Container는 각 종류의 Servlet당 한개의 객체만 사용
=> Front Controller인 DispatcherServlet은 싱글톤 객체임을 의
- Spring 은 Tomcat에서 한 종류의 Servlet(DispatcherServlet) 만을 사용
- Spring 에서 Controller 객체(Bean)는 Tomcat에서 Servlet객체와 동일하게 싱글톤으로 처리됨
0.1 어떻게 수많은 요청들을 단 하나의 객체로 커버할 수 있나?
:: 몇개이든지 N 개의 스레드는 단 하나의 싱글톤 객체만 구독(공유)
:: 각각의 스레드는 각자의 Method Stack에서 메서드를 수행
:: So, 모든 요청에 대해 각 스레드 영역으로 분리되어 동시 + 격리 수행 가능
=> 모든 요청에 대해 동시적으로 Stateless and Thread-Safe 유지 가능
0.2 각 단계에서의 Servlet 비교
구분 | Tomcat Servlet 싱글톤 |
Spring Controller 싱글톤
|
관리 주체 | Tomcat 컨테이너 | Spring 컨테이너 |
객체 생성 시점 | 웹 애플리케이션 로딩 시 |
Spring 컨테이너 초기화 시
|
객체 소멸 시점 | 웹 애플리케이션 종료 시 |
Spring 컨테이너 종료 시
|
1. FrontController의 발전 과정
구분 | Front Controller 이전의 Servlet : Java EE 시절 | Front Controller 적용된 Servlet : Spring 시절 |
방식 | | Servlet 은 요청 URL 마다 할당되어 개발 | | 단일 Servlet 이 요청 URL 마다 컨트롤러 호출 |
Ex) GET /hello POST /hello DELETE /world |
|
|
![]() |
![]() |
- 왜 Front Controller 가 필요성
:: 중앙화 = 중앙처리 + 중앙관리- Controller 호출의 중앙화
- HandlerMapping (중앙관리) : URL 마다 Controller 들을 정돈/검색
- HandlerAdaptor (중앙처리) : 앞서 찾은 Controller 호출을 담당
- 무조건 (공통적으로) Model 과 View 이름을 반환
- View 생성의 중앙화
- ViewResolver (중앙관리) : View 이름마다 Template 파일들을 정돈/검색
- View (중앙처리) : 앞서 찾은 Template 에 Model 을 합쳐 반환할 View 렌더링
- Controller 호출의 중앙화
2. Spring - Front Controller 상세 흐름
2.1 EC2 서버에서 Tomcat의 첫 구동 시, 2개의 Container가 생성
- Servlet Container (ServletContext)
:: Tomcat 이 처음 구동 시, 가장 먼저 ServletContext 생성
:: 다음 ServletContextListener 이 실행되어 아래 Spring Container (ApplicationContext) 생성 - Spring Container (ApplicationContext)
:: 해당에서도 2계층으로 다시 분리됨- Servlet WebApplicationContext
:: Servlet 에서만 사용하는 Bean - @Controller 등
:: Presentation Layer : Spring MVC 로직에 해당하는 객체들
:: HandlerMapping, ViewResolver, @Controller 등 - Root WebApplicationContext
:: 인프라, 비지니스 공통 Bean - @Service, @Repository 등
:: Business Layer, Data Access Layer
:: @Service, @Repository, @Compoentn, @Configuration 등
- Servlet WebApplicationContext
2.2 Tomcat 및 Container 들이 모두 생성된 뒤에는 클라이언트 요청을 받을 수 있다.
2.3 클라이언트 요청에 따라 Tomcat 은 정적 페이지가 존재하는지 확인
2.4 정적 페이지가 존재하지않는다면, Servlet Container 가 요청을 받아 Servlet 할당
- 단일 DispatchServlet 생성
:: 원래는 URL 규칙에 따라 다양한 Servlet 생성인데, Spring 에선 단일
2.5 DispatchServlet 은 Front Controller 로써 역할 수행
1. Controller 호출의 중앙화
- HandlerMapping (중앙관리) : URL 에 따른 Controller Bean 검색
- HandlerAdaptor (중앙처리) : 앞서 찾은 Controller Bean 호출에 대한 실행을 위임
==> Controller Bean 은 결과로 Model 과 View 이름을 반환
// ModelAndView 객체로 반환하거나 (구버전 Spring 에서)
@RequestMapping(method = RequestMethod.GET, value = "/")
public ModelAndView index() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("name", "Aaron");
modelAndView.addObject("age", 10);
modelAndView.setViewName("index.html");
return mav;
}
// Model 객체와 View 이름(String) 반환 (최근 Spring 방식)
@RequestMapping(method = RequestMethod.GET, value = "/")
public String index(Model model) {
model.addAttribute("name", "Aaron");
model.addAttribute("age", 10);
return "index.html";
}
2. View 생성의 중앙화
- ViewResolver (중앙관리) : Controller가 반환한 View 이름에 맞는 Template 검색
- View (중앙처리) : 앞서 찾은 Template 에 Model 객체를 합쳐 반환할 View 렌더링
- Thymeleaf
:: 현재 Spring 표준 Server-side Template Engine
:: ViewTemplate + Model = View 생성 - Client-side Template Engine : React, Vue 등
- Thymeleaf
2.6 최종적인 View를 클라이언트에 반환
참조
ASAC수업 자료
How does a single servlet handle multiple requests from client side
How does a single servlet handle multiple client requests coming in the form of user requests ? Based on the singleton design pattern I know we get a single instance of servlet created , but how do...
stackoverflow.com
추가로 보면 좋을 글들
Application-Context와 Servlet-Context | Carrey`s 기술블로그
들어가며 회사 업무 중에 AOP를 이용하여 개발 중에 AOP가 제대로 설정이 되지 않는 문제가 있었다. 문제의 원인은 Component-scan 위치에 따른 Bean 생성 위치에 있었다. core가 되는 프로젝트는 applicatio
jaehun2841.github.io
[SpringMVC] WebApplicationContext 정리
MVC에서의 WebApplicationContext
yeoooo.github.io
'정리용 > SpringBoot' 카테고리의 다른 글
[Spring] 3. SpringContext - Bean 등록 (1) | 2025.02.19 |
---|---|
[Spring] 3. Spring Context (0) | 2025.02.19 |
[Spring] 2-2-1. Spring MVC - Controller의 반환 값 (1) | 2025.02.07 |
[Spring] 2-2. Spring MVC - 구조와 동작 (0) | 2025.02.07 |
[Spring] 2-1. Spring MVC - HandlerMapping (0) | 2025.02.07 |
- Total
- Today
- Yesterday
- asac#asac7기
- useMemo
- memo
- acac
- useRef
- react
- ssh
- useEffect
- useCallback
- asac7기
- git
- useLayoutEffect
- Nginx
- useState
- acas#acas7기
- useContext
- asac7#asac
- asac7
- ASAC
- useReducer
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |