๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

Spring DispatcherServlet(๋””์ŠคํŒจ์ฒ˜์„œ๋ธ”๋ฆฟ) ๊ฐœ๋…๋ถ€ํ„ฐ ๋™์ž‘ ๊ณผ์ •๊นŒ์ง€

by ์ฃผ๋ฐœ2 2023. 2. 11.
๋ฐ˜์‘ํ˜•

๐Ÿ”— DispatcherServlet

์•ˆ๋…•ํ•˜์„ธ์š”, ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” Spring์—์„œ HTTP ์š”์ฒญ ๋ฐ ์‘๋‹ต๊ณผ ๊ด€๋ จํ•˜์—ฌ ๊ฐ€์žฅ ํ•ต์‹ฌ ๊ธฐ์ˆ ์ธ DispatcherServlet์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

 

ํ‰์†Œ์— ๊ฐœ๋…์— ๋Œ€ํ•ด์„œ๋Š” ์–ด๋Š ์ •๋„ ์•Œ๊ณ  ์žˆ์—ˆ์ง€๋งŒ ์–ด๋– ํ•œ ๊ณผ์ •์„ ํ†ตํ•ด ๋””์ŠคํŒจ์ฒ˜ ์„œ๋ธ”๋ฆฟ์ด ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›๊ณ , ๋ฉ”์„œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ •๋ณด๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‘๋‹ต์„ ๋ฐ˜ํ™˜ํ•˜๋Š”์ง€ ๋‚ด๋ถ€ ๋™์ž‘์€ ์ž˜ ๋ชฐ๋ž์—ˆ๋Š”๋ฐ์š”, ์ด๋ฒˆ ๊ธฐํšŒ์— ์ •๋ฆฌํ•ด ๋ณด์•˜์Šต๋‹ˆ๋‹ค.

(ํ‹€๋ฆฐ ๋‚ด์šฉ์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ์œผ๋‹ˆ ๋ง์”€ํ•ด ์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿ™)


| DispatcherServlet ๊ฐœ๋…

ํ”„๋ก ํŠธ ์ปจํŠธ๋กค๋Ÿฌ(Front Controller)๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” DispatcherServlet์€ HTTP ํ”„๋กœํ† ์ฝœ๋กœ ๋“ค์–ด์˜ค๋Š” ๋ชจ๋“  ์š”์ฒญ์„ ๋จผ์ € ๋ฐ›์•„์„œ ์ ํ•ฉํ•œ ์ปจํŠธ๋กค๋Ÿฌ์— ์œ„์ž„(Delegate request)๋Š” ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.

(์•„๋ž˜์ด๋ฏธ์ง€์—์„œ Front controller ๋ถ€๋ถ„์„ DispatcherServlet์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.)

https://docs.spring.io/spring-framework/docs/3.0.0.M4/spring-framework-reference/html/ch15s02.html

 

 

| DispatcherServlet ์žฅ์ 

๊ณผ๊ฑฐ DispatcherServlet์ด ๋“ฑ์žฅํ•˜๊ธฐ ์ „์—๋Š” ๋ชจ๋“  ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•ด URL์„ ๋งคํ•‘ํ•˜๊ธฐ ์œ„ํ•ด ๊ด€๋ จ ์„ค์ •์„ ํ•ด์ฃผ์–ด์•ผ ํ•˜๋Š” ๋ฒˆ๊ฑฐ๋กœ์›€์ด ์กด์žฌํ–ˆ์Šต๋‹ˆ๋‹ค. (web.xml ํŒŒ์ผ) >> ๋Œ“๊ธ€ ์ฐธ๊ณ  ๋ฐ”๋ž๋‹ˆ๋‹ค. ๐Ÿ™

 

๊ทธ๋Ÿฌ๋‚˜ DispatcherServlet์ด ๋“ฑ์žฅํ•œ ์ดํ›„์—๋Š” DispatcherServlet์ด ๋ชจ๋“  ์š”์ฒญ์„ ๋จผ์ € ๋ฐ›์•„์„œ ์ง์ ‘ ์ ํ•ฉํ•œ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ์œ„์ž„์„ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ์œ„์™€ ๊ฐ™์€ ์ž‘์—…์€ ํ•„์š”์—†์–ด์กŒ๊ณ , ์ด๋กœ ์ธํ•ด ๊ฐœ๋ฐœ์ด ๊ต‰์žฅํžˆ ํŽธ๋ฆฌํ•ด์กŒ์Šต๋‹ˆ๋‹ค.

 

๊ฐœ๋ฐœ์ž์˜ ์ž…์žฅ์—์„œ ๊ต‰์žฅํžˆ ํŽธ๋ฆฌํ•ด์ง„ ๋งŒํผ DispatcherServlet์˜ ๋‚ด๋ถ€ ์ฝ”๋“œ๋Š” ๊ต‰~์žฅํžˆ ์ถ”์ƒํ™”๊ฐ€ ๋˜์–ด์žˆ๊ณ  ๋ณต์žกํ•œ๋ฐ์š”, ์–ด๋– ํ•œ ๊ณผ์ •์„ ๊ฑฐ์ณ ์œ„์™€ ๊ฐ™์€ ๋งˆ๋ฒ•(?)์„ ๋ถ€๋ฆฌ๊ฒŒ ํ•ด์ฃผ๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

| DispatcherServlet ๋™์ž‘ ๊ณผ์ •

https://terasolunaorg.github.io/guideline/5.0.1.RELEASE/en/Overview/SpringMVCOverview.html#overview-of-spring-mvc-processing-sequence

DispatcherServlet์˜ ๋™์ž‘ ๊ณผ์ •์— ๋Œ€ํ•ด ์กฐ๊ธˆ ๋” ์ž์„ธํžˆ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. DispatcherServlet์ด ํด๋ผ์ด์–ธํŠธ์˜ ๋ชจ๋“  ์š”์ฒญ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.
  2. ์š”์ฒญ ์ •๋ณด์— ๋Œ€ํ•ด HandlerMappinng์— ์œ„์ž„ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•  Handler(Controller)์„ ์ฐพ์Šต๋‹ˆ๋‹ค.
  3. 2๋ฒˆ์—์„œ ์ฐพ์€ Handler์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” HandlerAdapter๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.
  4. HandlerAdapter๋Š” Controller์— ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฒ˜๋ฆฌ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.
  5. Controller๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๊ณ , ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ๋ฅผ Model์— ์„ค์ •ํ•˜๋ฉฐ HandlerAdapter์— view name์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
    • ๋ชจ๋ธ์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด View๊ฐ€ ๋ Œ๋”๋ง์ด ๋˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ(ex. @RestController ๋“ฑ) View๊ฐ€ ๋ Œ๋”๋ง์ด ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. 
  6. 5๋ฒˆ์—์„œ ๋ฐ˜ํ™˜๋ฐ›์€ view name์„ ViewResolver์—๊ฒŒ ์ „๋‹ฌํ•˜๊ณ , ViewResolver์€ ํ•ด๋‹นํ•˜๋Š” View ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  7. DispatcherServlet์€ View์—๊ฒŒ Model์„ ์ „๋‹ฌํ•˜๊ณ  ํ™”๋ฉด ํ‘œ์‹œ๋ฅผ ์š”์ฒญํ•ฉ๋‹ˆ๋‹ค.
  8. ์ตœ์ข…์ ์œผ๋กœ ์„œ๋ฒ„์˜ ์‘๋‹ต์„ ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

๐Ÿ”— DispatcherServlet ๋‚ด๋ถ€ ์ฝ”๋“œ

DispatcherServlet์˜ ๋‚ด๋ถ€ ์ฝ”๋“œ๋ฅผ ์‚ดํŽด๋ณด๋ฉด Spring ํŒ€์—์„œ ๊ฐœ๋ฐœํ•œ ์—ฌ๋Ÿฌ ๋””์ž์ธ ํŒจํ„ด์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ํŒจํ„ด๋“ค์€ ์ „๋ถ€ ์ฝ”๋“œ์—์„œ ๋‚˜์˜ค๋Š” ๊ฐœ๋…๋“ค์ด๋‹ˆ ๊ฐ„๋žตํžˆ ์‚ดํŽด๋ณด์‹œ๊ณ  ํ•ด๋‹น ํฌ์ŠคํŒ…์„ ๋ณด์‹œ๋ฉด ์ดํ•ดํ•˜์‹œ๊ธฐ์— ๋ณด๋‹ค ์ˆ˜์›”ํ•  ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค. ๐Ÿ˜…


์ž ๊ทธ๋Ÿผ ๊ธธ๊ณ ๋„ ๊ธด DispatcherServlet ๋‚ด๋ถ€ ์†์œผ๋กœ์˜ ์—ฌ์ •์„ ๋– ๋‚˜ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค..

 

DispatcherServrlet์€ Spring MVC์˜ ํ•ต์‹ฌ ์š”์†Œ๋กœ ์œ„ ์‚ฌ์ง„๊ณผ ๊ฐ™์€ ๊ณ„์ธต ๊ตฌ์กฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

(DispatcherServlet -> FrameworkServlet -> HttpServletBean -> HttpServlet -> GenericServlet -> Servlet)

 

Servlet

  • Servlet์€ ์›น ์„œ๋ฒ„ ๋‚ด์—์„œ ์‹คํ–‰๋˜๋Š” ์ž๋ฐ” ํ”„๋กœ๊ทธ๋žจ์ž…๋‹ˆ๋‹ค.
  • HTTP ํ†ต์‹ ์„ ์‚ฌ์šฉํ•˜์—ฌ ์›น ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ์š”์ฒญ์„ ๋ฐ›๊ณ  ์‘๋‹ตํ•˜๋Š” ๊ธฐ์ˆ ์ž…๋‹ˆ๋‹ค.
  • ์ผ๋ฐ˜์ ์ธ ์„œ๋ธ”๋ฆฟ์„ ์ƒ์†ํ•˜๋Š” GenericServlet, HTTP ์„œ๋ธ”๋ฆฟ์„ ์ƒ์†ํ•˜๋Š” HttpServlet์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค.
  • ์„œ๋ธ”๋ฆฟ์˜ ์ƒ๋ช…์ฃผ๊ธฐ - init(), service(), destory()
  • Servlet์— ๋Œ€ํ•œ ์ถ”๊ฐ€์ ์ธ ๋‚ด์šฉ์€ ํ•ด๋‹น ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”.

GenericServlet

  • ํ”„๋กœํ† ์ฝœ์— ๋…๋ฆฝ์ ์ธ ์ผ๋ฐ˜ ์„œ๋ธ”๋ฆฟ์ž…๋‹ˆ๋‹ค.
  • ์›น์—์„œ ์‚ฌ์šฉํ•  HTTP ์„œ๋ธ”๋ฆฟ์„ ์ž‘์„ฑํ•˜๋ ค๋ฉด HttpServlet์„ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค.

HttpServlet

  • ์›น ์‚ฌ์ดํŠธ์— ์ ํ•ฉํ•œ HTTP ์„œ๋ธ”๋ฆฟ ์ž…๋‹ˆ๋‹ค.
  • HttpServlet์˜ ํ•˜์œ„ ํด๋ž˜์Šค๋Š” ์•„๋ž˜ ๋ฉ”์„œ๋“œ ์ค‘ ์ตœ์†Œ ํ•˜๋‚˜์˜ ๋ฉ”์„œ๋“œ๋Š” ์žฌ์ •์˜๋ฅผ ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
    • doGet(), doPost(), doPut(), doDelete()
    • init(), destory(): ์„œ๋ธ”๋ฆฟ ๋ผ์ดํ”„ ์‚ฌ์ดํด์˜ ์ดˆ๊ธฐํ™” ๋ฐ ํŒŒ๊ธฐ
    • getServletInfo(): ์„œ๋ธ”๋ฆฟ์— ๋Œ€ํ•œ ์ •๋ณด

HttpServletBean

  • HttpServlet์˜ ๋‹จ์ˆœ ํ™•์žฅ ํด๋ž˜์Šค๋กœ Spring์ด ๊ตฌํ˜„ํ•œ ๋ชจ๋“  ์„œ๋ธ”๋ฆฟ ์œ ํ˜•์— ์ ํ•ฉํ•œ ์„œ๋ธ”๋ฆฟ (spring ํŒจํ‚ค์ง€)

FrameworkServlet

  • ์Šคํ”„๋ง ์›น ํ”„๋ ˆ์ž„์›Œํฌ์˜ ๊ธฐ๋ณธ ์„œ๋ธ”๋ฆฟ์ž…๋‹ˆ๋‹ค.
  • ํ•˜์œ„ ํด๋ž˜์Šค๋Š” ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด doService() ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

DispatcherServlet

  • HTTP ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ค‘์•™ ์ง‘์ค‘ํ˜• Dispatcher (Front-Controller)
  • ์ ์ ˆํ•œ ์–ด๋Œ‘ํ„ฐ ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด ์–ด๋– ํ•œ workflow์™€๋„ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•  ๋งŒํผ ๋งค์šฐ ์œ ์—ฐํ•ฉ๋‹ˆ๋‹ค.
  • Mapping, Adapter, Exception ๊ด€๋ จ ํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.
    • HandlerMapping - RequestMappingHandlerMapping
    • HandlerAdapter - RequestMappingHandlerAdapter
    • HandlerExceptionResolver - ExceptionHandlerExceptionResolver

 

์‹ค์ œ๋กœ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ DispatcherServlet์ด ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •์„ ํ•˜๋‚˜์”ฉ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

(API ์ •๋ณด, ํด๋ž˜์Šค ๋ฐ ๋ฉ”์„œ๋“œ ์ •๋ณด, ์ฟผ๋ฆฌ ํŒŒ๋ผ๋ฏธํ„ฐ ๋“ฑ๋“ฑ)

 

์˜ˆ์ œ์— ์‚ฌ์šฉ๋  ๊ฐ„๋‹จํ•œ API๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๊ณ , ํ˜ธ์ถœ URI๋Š” '/test?name=juhyun' ์ž…๋‹ˆ๋‹ค.

 

DispatcherServlet์ด ์š”์ฒญ์„ ๋ฐ›์•„์„œ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ณผ์ •์„ ํฌ๊ฒŒ ์‚ดํŽด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

1. HttpServlet: ์„œ๋ธ”๋ฆฟ์˜ Request/Response๋ฅผ HttpServlet์˜ Request/Response๋กœ ๋ณ€ํ™˜

2~5. FrameworkServlet: ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์˜ Http Method์— ๋”ฐ๋ผ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ - doXXX ๋ฉ”์„œ๋“œ

6. DispatcherServlet: ์š”์ฒญ ์ •๋ณด์— ๋Œ€ํ•ด ์ด๋ฅผ ์ฒ˜๋ฆฌํ•  Handler๋ฅผ ์ฐพ๊ณ , HandlerAdapter๋ฅผ ํ†ตํ•ด Controller์— ์š”์ฒญ ์œ„์ž„

    6-1. ์š”์ฒญ์— ๋งคํ•‘๋˜๋Š” HandlerMapping(HandlerExecutionChain) ์กฐํšŒ

    6-2. Handler์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” HandlerAdapter ์กฐํšŒ

    6-3. HandlerAdapter๋ฅผ ํ†ตํ•ด ์‹ค์ œ Controller์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ํ˜ธ์ถœ

 

1. HttpServlet: Servlet์˜ Request, Response๋ฅผ HttpServlet์˜ Request,Response๋กœ ๋ณ€ํ™˜

  • HttpServlet.service()

HttpServlet

๋จผ์ € ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์€ DispatcherServlet์˜ ๋ถ€๋ชจ ์ถ”์ƒ ํด๋ž˜์Šค์ธ HttpServlet์˜ service() ๋ฉ”์„œ๋“œ์—์„œ ์š”์ฒญ์„ ๋ฐ›์Šต๋‹ˆ๋‹ค.

์œ„ ์ฝ”๋“œ์˜ 13, 14๋ผ์ธ์—์„œ ์„œ๋ธ”๋ฆฟ์˜ ์š”์ฒญ ๋ฐ ์‘๋‹ต์„ HttpServlet์˜ ์š”์ฒญ ๋ฐ ์‘๋‹ต์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

์ดํ›„ 18๋ฒˆ์งธ ๋ผ์ธ์˜ service() ๋ฉ”์„œ๋“œ๋Š” FrameworkServlet ์ถ”์ƒ ํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋“œ๋œ service() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

์œ„ req, res๋Š” Facade ํด๋ž˜์Šค์ด๋ฉฐ HttpServletRequest, HttpServletResponse ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€ํ™˜์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. 

 

2. FrameworkServlet: HTTP ๋ฉ”์„œ๋“œ์— ๋”ฐ๋ผ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ

  • HttpServlet.service() -> FrameworkServlet.service()

FrameworkServlet

Http Method๊ฐ€ PATCH์ผ ๊ฒฝ์šฐ processRequest() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , ๊ทธ ์™ธ์˜ ๋ฉ”์„œ๋“œ๋Š” 14๋ผ์ธ์˜ super.service() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค. (super = HttpServlet)

 

์•„๋ž˜์—์„œ ์‚ดํŽด๋ณด๊ฒ ์ง€๋งŒ doXXX ๋ฉ”์„œ๋“œ์—์„œ doPatch() ๋ผ๋Š” ๋ฉ”์„œ๋“œ๋งŒ ์œ ์ผํ•˜๊ฒŒ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋ฐ์š”, ์ด์œ ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

Servlet api 3.0์„ ์ •์˜ํ•œ JSR 315์—์„œ patch ๋ฉ”์„œ๋“œ์— ๋Œ€ํ•ด ์ธ์‹ํ•˜์ง€ ๋ชปํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— doPatch() ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์ง€ ๋ชปํ–ˆ๊ณ , ์ดํ›„ PATCH ๋ฉ”์„œ๋“œ๊ฐ€ HTTP ํ”„๋กœํ† ์ฝœ์— ์ถ”๊ฐ€๋˜๋ฉด์„œ Spring ํŒจํ‚ค์ง€์— ์†ํ•˜๋Š” FrameworkServlet ํด๋ž˜์Šค์—์„œ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ†ตํ•ด Http Method๊ฐ€ PATCH์ธ ์ผ€์ด์Šค์™€ ๊ทธ ์™ธ์˜ ์ผ€์ด์Šค๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋Œ€์‘ํ–ˆ์Šต๋‹ˆ๋‹ค.
- https://stackoverflow.com/questions/47080677/java-httpservlet-patch-method

ํ˜„์žฌ ์š”์ฒญํ•œ Http Method๋Š” GET์ด๋ฏ€๋กœ 14๋ผ์ธ์˜ super.service() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๋‹ค์‹œ HttpServlet ํด๋ž˜์Šค์˜ service() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

 

3. HttpServlet: service()

  • HttpServlet.service() -> FrameworkServlet.service() -> HttpServlet.service()

HttpServlet

HttpServlet ์ถ”์ƒ ํด๋ž˜์Šค์˜ service() ๋ฉ”์„œ๋“œ๋Š” Http Method์— ๋”ฐ๋ผ doXXX ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

์‹ค์ œ ํ˜ธ์ถœ์€ HttpServlet ํด๋ž˜์Šค๋ฅผ ์ƒ์†๋ฐ›๊ณ , doXXX ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉํ•˜๊ณ  ์žˆ๋Š” ์ž์‹ ํด๋ž˜์Šค์˜ doXXX ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

(์—ฌ๋‹ด์ด์ง€๋งŒ ์œ„ lastModified ๊ฐ’์€ ํ•ญ์ƒ -1์ธ๋ฐ, ๋”ฐ๋กœ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด๋†“์€ ์ด์œ ๊ฐ€ ๊ถ๊ธˆํ•˜๋„ค์š”.. ๐Ÿง)

 

HttpServlet ์ถ”์ƒ ํด๋ž˜์Šค์—์„œ ํ…œํ”Œ๋ฆฟ์„ ์ œ๊ณตํ•˜๊ณ , ์‹ค์ œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์€ ์ด๋ฅผ ์ƒ์†๋ฐ›๋Š” FrameworkServlet์—์„œ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š”๋ฐ์š”, ์ด๋Š” ๋””์ž์ธ ํŒจํ„ด์—์„œ ํ…œํ”Œ๋ฆฟ ๋ฉ”์„œ๋“œ ํŒจํ„ด์ด ์ ์šฉ๋˜์—ˆ์Œ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

4. FrameworkServlet: doXXX() -> processRequest() -> doService()

  • HttpServlet.service() -> FrameworkServlet.service() -> HttpServlet.service() -> FrameworkServlet.doXXX(), processRequest(), doService()

FrameworkServlet

์ž์‹ ํด๋ž˜์Šค์—์„œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ๋œ doXXX ๋ฉ”์„œ๋“œ๋Š” ์œ„์™€ ๊ฐ™์ด ๋ชจ๋‘ processRequest() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.

 

FrameworkServlet

FrameworkServlet ์ถ”์ƒ ํด๋ž˜์Šค์˜ processRequest() ๋ฉ”์„œ๋“œ์—์„œ ํ•ต์‹ฌ ๋กœ์ง์€ 7๋ฒˆ์งธ ๋ผ์ธ์˜ doService() ์ž…๋‹ˆ๋‹ค.

doService() ๋ฉ”์„œ๋“œ๊ฐ€ ๋“œ๋””์–ด DispatcherServlet ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์ง„์ž…์ ์ž…๋‹ˆ๋‹ค!

 

๊ทธ๋Ÿผ DispatcherServlet์˜ doService() ๋ฉ”์„œ๋“œ์—์„œ๋Š” ์–ด๋– ํ•œ ๊ณผ์ •์ด ์ผ์–ด๋‚˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

5. DispatcherServlet: doService()

  • HttpServlet.service() -> FrameworkServlet.service() -> HttpServlet.service() -> FrameworkServlet.doXXX(), processRequest(), doService() -> DispatcherServlet.doService(), doDispatch()

doService() ๋ฉ”์„œ๋“œ์—์„œ๋Š” Request์— ๋Œ€ํ•ด log๋ฅผ ๋‚จ๊ธฐ๊ณ , ๋ณต์›ํ•  ์ˆ˜ ์žˆ๋„๋ก ์š”์ฒญ ์ •๋ณด์— ๋Œ€ํ•ด snapshot์„ ๋‚จ๊ธฐ๋Š” ๋“ฑ ๋กœ์ง์ด ์กด์žฌํ•˜์ง€๋งŒ, ํ•ต์‹ฌ ๋กœ์ง์€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ํŒจ์Šคํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ํ•ด๋‹น ๋ฉ”์„œ๋“œ์—์„œ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ๋ถ€๋ถ„์€ 16๋ฒˆ์งธ ๋ผ์ธ์˜ doDispatch() ๋ฉ”์„œ๋“œ์ธ๋ฐ์š”, ์ด ํ•จ์ˆ˜์—์„œ ์•„๋ž˜์˜ ์ž‘์—…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

  • ์š”์ฒญ์— ๋งคํ•‘๋˜๋Š” HandlerMapping(HandlerExecutionChain) ์กฐํšŒ
  • Handler์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” HandlerAdapter ์กฐํšŒ
  • HandlerAdapter๋ฅผ ํ†ตํ•ด Controller ํ˜ธ์ถœ (์‹ค์ œ ๋กœ์ง)
DispatcherServlet.doDispatch()
  - Process the actual dispatching to the handler.
  - The handler will be obtained by applying the servlet's HandlerMappings in order. The HandlerAdapter will be obtained by querying the servlet's installed HandlerAdapters to find the first that supports the handler class.
  - All HTTP methods are handled by this method. It's up to HandlerAdapters or handlers themselves to decide which methods are acceptable.

https://terasolunaorg.github.io/guideline/5.0.1.RELEASE/en/Overview/SpringMVCOverview.html#overview-of-spring-mvc-processing-sequence

 

6. DispatcherServlet: doDispatch() - HandlerMapping, HandlerAdapter, Handle Controller

  • HttpServlet.service() -> FrameworkServlet.service() -> HttpServlet.service() -> FrameworkServlet.doXXX(), processRequest(), doService() -> DispatcherServlet.doService(), doDispatch() - HandlerMapping, HandlerAdapter, handle Controller

doDispatch() ๋ฉ”์„œ๋“œ๋Š” ์œ„์—์„œ ๋ง์”€๋“œ๋ ธ๋“ฏ์ด ์„ธ ๊ฐ€์ง€ ํ•ต์‹ฌ ์ž‘์—…์„ ์ง„ํ–‰ ํ•ฉ๋‹ˆ๋‹ค.

  • 18 ๋ผ์ธ: HandlerMapping(HandlerExecutionChain) ์กฐํšŒ
  • 26๋ผ์ธ: Handler์„ ์ˆ˜ํ–‰ํ•  HandlerAdapter ์กฐํšŒ
  • 45๋ผ์ธ: HandlerAdapter๋ฅผ ํ†ตํ•ด Controller์˜ ์‹ค์ œ ๋กœ์ง ํ˜ธ์ถœ

 

6-1. ์š”์ฒญ์— ๋งคํ•‘๋˜๋Š” HandlerMapping(HandlerExecutionChain) ์กฐํšŒ

DispatcherServlet

HandlerExecutionChain์€ HandlerMethod, Interceptor๋“ค๋กœ ๊ตฌ์„ฑ๋˜์–ด HandlerMapping์— ํ•ด๋‹นํ•˜๋Š” ํด๋ž˜์Šค ์ž…๋‹ˆ๋‹ค.

HandlerExecutionChain
 - Handler execution chain, consisting of handler object and any handler interceptors.
 - Returned by HandlerMapping's HandlerMapping.getHandler method.

DispatcherServlet์ด HandlerExecutionChain ๊ฐ์ฒด๋ฅผ ์–ป๋Š” ์ด์œ ๋Š” doDispatch() ๋ฉ”์„œ๋“œ์— ๋“ฑ๋ก๋œ 
์ธํ„ฐ์…‰ํ„ฐ๋ฅผ ์ฒ˜๋ฆฌ ํ•˜๋Š” ๋กœ์ง์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.(applyPreHandle, applyPostHandle)

์œ„ ์ฝ”๋“œ์—์„œ 5๋ฒˆ ๋ผ์ธ์˜ mapping.getHandler(request) ๋ฉ”์„œ๋“œ๋ฅผ ๋‚ด๋ถ€๋กœ ๊ณ„์† ๋“ค์–ด๊ฐ€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

(DispatcherServlet.getHandler() -> AbstractHandlerMapping.getHandler() -> RequestMappingInfoHandlerMapping.getHandlerInternal() -> AbstractHandlerMethodMapping.getHandlerInternal())

getHandler() ๋ฉ”์„œ๋“œ๋ฅผ ๊ณ„์† ํƒ€๊ณ  ๋“ค์–ด๊ฐ€๋ณด๋ฉด AbstractHandlerMethodMapping ์ถ”์ƒ ํด๋ž˜์Šค์˜ getHandlerInternal() ๋ฉ”์„œ๋“œ์—์„œ HandlerMethod๋ฅผ ์ฐพ๊ณ , AbstractHandlerMapping ์ถ”์ƒ ํด๋ž˜์Šค์—์„œ HandlerExecutionChain ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฆฌํ„ดํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • lookupPath: URI
  • mappingRegistry: ๋งคํ•‘ ์ •๋ณด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ํด๋ž˜์Šค
  • handlerMethod: bean, beanFactory, Controller์˜ method ์ •๋ณด ๋“ฑ

 

์ด์ œ ๋น ์ ธ๋‚˜์™€์„œ AbstractHandlerMapping.getHandler() ๋ถ€๋ถ„์„ ์‚ดํŽด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

getHandlerInternal() ๋ฉ”์„œ๋“œ๋ฅผ ์ˆ˜ํ–‰ํ•œ ํ›„ handler์˜ ํƒ€์ž…์€ HandlerMethod ์ด๊ณ , ์ˆ˜ํ–‰๋  ์ˆ˜ ์žˆ๋Š” ์ธํ„ฐ์…‰ํ„ฐ๋“ค์„ ํฌํ•จํ•˜์—ฌ getHandlerExecutionChain() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด HandlerExecutionChain ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. (13๋ผ์ธ, 22๋ผ์ธ ~)

 

์œ„์™€ ๊ฐ™์€ ๊ณผ์ •์„ ํ†ตํ•ด HandlerMapping์„ ์ฐพ๊ณ , HandlerExecutionChain ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์Œ์œผ๋กœ HandlerExecutionChain์„ ํ†ตํ•ด HandlerAdapter๋ฅผ ์ฐพ๋Š” ๊ณผ์ •์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

6-2. Handler์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” HandlerAdapter ์กฐํšŒ (28๋ฒˆ์งธ ๋ผ์ธ)

getHandlerAdapter(): HandlerExecutionChain๋ฅผ ํ†ตํ•ด HandlerAdapter๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค.

์˜ˆ์ œ ์ฝ”๋“œ์—์„œ ์ž‘์„ฑํ–ˆ๋˜ @GetMapping ์–ด๋…ธํ…Œ์ด์…˜ ๋‚ด๋ถ€์— @RequestMapping ์–ด๋…ธํ…Œ์ด์…˜์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์œ„์˜ for๋ฌธ์—์„œ๋Š” RequestMappingHandlerAdapter ์–ด๋Œ‘ํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

(๋„ค์ด๋ฐ๋งŒ ๋ด๋„ ํด๋ž˜์Šค์˜ ์—ญํ• ์ด ์œ ์ถ”๊ฐ€ ๊ฐ€๋Šฅํ•˜๋„ค์š”,, Spring ์—ญ์‹œ ๋Œ€๋‹จํ•˜๋„ค์š”.. ๐Ÿ™)

 

๋˜ํ•œ ์œ„์—์„œ handler์€ HandlerMethod๋กœ ๋งคํ•‘๋˜๋Š” ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์„œ๋“œ ๋ฐ ๋นˆ, ๋นˆ ํŒฉํ† ๋ฆฌ ๋“ฑ์ด ์ €์žฅ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

6-3. HandlerAdapter๋ฅผ ํ†ตํ•ด ์‹ค์ œ Controller์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ํ˜ธ์ถœ (40๋ฒˆ์งธ ๋ผ์ธ)

6-1, 6-2์—์„œ HandlerExecutionChain, HandlerAdapter๋ฅผ ์ฐพ๋Š” ๊ณผ์ •์„ ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

(๋‘ ๊ณผ์ •๋„ ๋ณต์žกํ–ˆ์ง€๋งŒ ์ด๋ฒˆ 6-3 ๊ณผ์ •์ด ๋” ๋ณต์žกํ•ฉ๋‹ˆ๋‹ค...๐Ÿ˜ญ)

 

๋งˆ์ง€๋ง‰์œผ๋กœ HandlerAdapter๋ฅผ ํ†ตํ•ด ์‹ค์ œ Controller์˜ ๋กœ์ง์„ ์‹คํ–‰ํ•˜๋Š”(invoke) ๊ณผ์ •์„ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

HandlerAdapter๊ฐ€ Controller๋ฅผ ์‹คํ–‰ํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ์–ป๋Š” ๊ณผ์ •์€ ๋งŽ์ด ๋ณต์žกํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋งŽ์€ ์ธ๋‚ด์‹ฌ(?)์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค... ๐Ÿ˜‚

 

HandlerAdapter์˜ handle() ๋ฉ”์„œ๋“œ๋ฅผ ํƒ€๊ณ  ๋“ค์–ด๊ฐ€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. 

RequestMappingHandlerAdapter ํด๋ž˜์Šค์˜ handleInternal() ๋ฉ”์„œ๋“œ์—์„œ Controller๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋กœ์ง์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. (invokeHandlerMethod())

 

๊ทธ๋ž˜์„œ invokeHandlerMethod() ๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€๋Š” ..?

RequestMappingHandlerAdapter (์ด์ œ ๊ฑฐ์˜ ๋‹ค ์™”์Šต๋‹ˆ๋‹ค....)

invokeHandlerMethod ํ•จ์ˆ˜์—์„œ ์‚ดํŽด๋ณผ ๋ถ€๋ถ„์€ ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ์ž…๋‹ˆ๋‹ค.

  • 10๋ฒˆ์งธ ๋ผ์ธ: ServletInvocableHandlerMethod invocableMethod = createInvocableHandlerMethod(...)
  • 23๋ฒˆ์งธ ๋ผ์ธ: invokeAndHandle(...)

10๋ฒˆ์งธ ๋ผ์ธ์—์„œ createInvocableHandlerMethod(handlerMethod) ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ServletInvocableHandlerMethod ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ServletInvocableHandlerMethod ํด๋ž˜์Šค๋Š” ๋ฌด์—‡์ผ๊นŒ์š”??

HandlerMethod๋ฅผ ์ƒ์†๋ฐ›๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค ์ค‘ ํ•˜๋‚˜์ž…๋‹ˆ๋‹ค. 

 

ํด๋ž˜์Šค ์ƒ๋‹จ์˜ ์ฃผ์„์„ ์„ค๋ช…ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค. (์˜ค์—ญ์ด ์กด์žฌํ• ์ˆ˜๋„..?)

๋“ฑ๋ก๋œ HandlerMethodReturnValueHandler๋ฅผ ํ†ตํ•ด ๋ฐ˜ํ™˜ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ธฐ๋Šฅ์œผ๋กœ InvocableHandlerMethod๋ฅผ ์ƒ์†๋ฐ›๊ณ , ๋ฉ”์„œ๋“œ ๋ ˆ๋ฒจ์—์„œ์˜ @ResponseStatus ์–ด๋…ธํ…Œ์ด์…˜์„ ํ†ตํ•ด ์‘๋‹ต ์ƒํƒœ๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ InvocableHandlerMethod๋Š”??

HandlerMethodArgumentResolver ๋ฆฌ์ŠคํŠธ๋ฅผ ํ†ตํ•ด ํ˜„์žฌ HTTP ์š”์ฒญ์—์„œ ๋ฐ›์€ ์ธ์ˆ˜ ๊ฐ’์œผ๋กœ ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœ(invoke)ํ•˜๋Š” HandlerMethod๋ฅผ ์ƒ์†๋ฐ›๋Š” ํด๋ž˜์Šค์ž…๋‹ˆ๋‹ค.

์•„ํ•˜! ์™„๋ฒฝํ•˜๊ฒŒ ์ดํ•ด๋ฅผ ํ•œ ๊ฑด ์•„๋‹ˆ์ง€๋งŒ, ServletInvocableHandlerMethod๋Š” HandlerMethod์™€ ํ•จ๊ป˜ ์ปจํŠธ๋กค๋Ÿฌ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ(HandlerMethodArgumentResolver) ๋ฐ ๋ฐ˜ํ™˜๊ฐ’(HandlerMethodReturnValueHandler)์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ๊ฐ์ฒด๋ผ๊ณ  ๋งํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์„ค๋ช…์ด ๊ธธ์–ด์กŒ๋„ค์š”! ๋‹ค์‹œ RequestMappingHandlerAdapter์˜ ์ฝ”๋“œ๋กœ ๋Œ์•„๊ฐ€๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

RequestMappingHandlerAdapter (์ด์ œ ๊ฑฐ์˜ ๋‹ค ์™”์Šต๋‹ˆ๋‹ค....)

์œ„ 10๋ฒˆ์งธ ๋ผ์ธ์˜ createInvocableHandlerMethod() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ServletInvocableHandlerMethod ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋”ฐ๋ผ๊ฐ€๋ณด๋ฉด bean, beanFactory, method, parameters ๋“ฑ์˜ ๋ณ€์ˆ˜๊ฐ€ ์ดˆ๊ธฐํ™”๋˜์–ด ์ƒ์„ฑ์ด ๋ฉ๋‹ˆ๋‹ค.

ServletInvocableHandlerMethod -> InvocableHandlerMethod -> HandlerMethod

 

ServletInvocableHandlerMethod ๊ฐ์ฒด์˜ ์ƒ์„ฑ ๋ฐ ์„ค์ •์ด ๋๋‚˜๋ฉด ์œ„ 23๋ฒˆ์งธ ๋ผ์ธ์˜ invokeAndHandle() ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

ServletInvocableHandlerMethod

invokeAndHandle()๋Š” Controller์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ณ  ๋ฐ˜ํ™˜ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

(์—ฌ๊ธฐ์—์„œ ๋ฆฌํ„ด ํƒ€์ž…์— ๋”ฐ๋ผ ModelAndView, ReponseEntity, ResponseBody ๋“ฑ์„ ๊ฐ Handler๊ฐ€ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.)

 

์‹ค์ œ ์ฒ˜๋ฆฌ๋Š” ๋ถ€๋ชจ ํด๋ž˜์Šค InvocableHandlerMethod์˜ invokeForRequest() ๋ฉ”์„œ๋“œ์—์„œ ์ด๋ฃจ์–ด์ง€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์ด์ฏค ๋˜๋ฉด ์ฒ˜์Œ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์š”์ฒญํ•œ URI๊ฐ€ ๋ฌด์—‡์ธ์ง€ ๊ธฐ์–ต์ด ๋‚˜์ง€ ์•Š์„ ์ˆ˜๋„ ์žˆ๊ธฐ์— ๊ธฐ์–ต์„ ๋˜์‚ด๋ ค๋ณด๋ฉด.... ๐Ÿ˜ญ

URI์™€ Controller์˜ test() ๋ฉ”์„œ๋“œ๋ฅผ ๋ฆฌ๋งˆ์ธ๋“œ ํ•˜๋Š” ์ด์œ ๋Š” ๋“œ๋””์–ด! ๋ฉ”์„œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ๊ฐ’์ด ์•„๋ž˜ ๋กœ์ง์— ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

 

์ž ๊ทธ๋Ÿผ ๋‹ค์‹œ InvocableHandlerMethod์˜ invokeForRequest() ๋ฉ”์„œ๋“œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

InvocableHandlerMethod

invokeForRequest() ๋ฉ”์„œ๋“œ์—์„œ๋Š” getMethodArgumentValues() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด Controller์˜ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด ํ•„์š”ํ•œ ์ธ์ž ๊ฐ’๋“ค์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

 

์œ„ ์˜ˆ์ œ์—์„œ๋Š” test() ๋ฉ”์„œ๋“œ์— ์ฟผ๋ฆฌ ์ŠคํŠธ๋ง์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด @RequestParam ์–ด๋…ธํ…Œ์ด์…˜์ด ์„ ์–ธ๋˜์–ด ์žˆ๋Š”๋ฐ ์ด ์™ธ์—๋„ @PathVariable, @RequestHeader ๋“ฑ์˜ ์–ด๋…ธํ…Œ์ด์…˜์ด ์Šคํ”„๋ง์˜ ArgumentResolver์— ์˜ํ•ด ์ฒ˜๋ฆฌ๊ฐ€ ๋˜๊ณ , ์ด์™€ ๊ด€๋ จํ•œ ์ž‘์—…์ด ์ˆ˜ํ–‰๋˜๋Š” ๊ณณ์ด getMethodArgumentValues() ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

 

getMethodArgumentValues() ๋ฉ”์„œ๋“œ์˜ ๋„ค์ด๋ฐ์„ ํ†ตํ•ด ๋ฉ”์„œ๋“œ์˜ ์ธ์ž ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค๋Š” ๊ฒƒ์„ ์œ ์ถ”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์‹ค์ œ ๊ฐ€์ ธ์˜ค๋Š” ๊ณผ์ •์„ ํƒ€๊ณ  ๋“ค์–ด๊ฐ€ ๋ณด๋ฉด ๋‹ค์Œ ํด๋ž˜์Šค๋“ค์„ ๊ฑฐ์ณ์„œ ์ธ์ž ๊ฐ’์„ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค... ๐Ÿ˜ฑ

  • InvocableHandlerMethod: this.resolvers.resolveArgument() -- (์œ„ ์ฝ”๋“œ์—์„œ 32๋ฒˆ์งธ ๋ผ์ธ)
  • HandlerMethodArguemtnResolverComposite: resolveArgument()
  • AbstractNamedValueMethodArgumentResolver: resolveArgument()
  • RequestParamMethodArguemtnResolver: resolveName()
  • ServletWebRequest: getParameterValues()
  • RequestFacade: getParameterValues()
  • Request: getParameterValues()
  • Paramteters: getParameterValues() --- apache.tomcat.util.http

์œ„์™€ ๊ฐ™์ด ํƒ€๊ณ  ํƒ€๊ณ  ํƒ€๊ณ  .... ๊ณ„์† ๋“ค์–ด๊ฐ€๋‹ค๋ณด๋ฉด apache.tomcat์˜ Parametersํด๋ž˜์Šค์˜ getParameterValues() ๋ฉ”์„œ๋“œ์—์„œ Map์„ ์ด์šฉํ•ด @RequestParam์˜ key์— ํ•ด๋‹นํ•˜๋Š” value๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

 

์ธ์ž๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋‚˜๋ฉด InvocableHandlerMethod๋กœ ๋Œ์•„์™€์„œ doInvoke() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ฉ๋‹ˆ๋‹ค.

์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋ฉด ์ดํ›„ ์‘๋‹ต์— ๋Œ€ํ•œ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ธฐ ์œ„ํ•ด ServletInvocableHandlerMethod์—์„œ HandlerMethodReturnValueHandlerComposite๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ›„์ฒ˜๋ฆฌ๋ฅผ ์ง„ํ–‰ ํ•ฉ๋‹ˆ๋‹ค.

(์•„๋ž˜ ์ฝ”๋“œ์—์„œ 135๋ฒˆ์งธ ๋ผ์ธ ~ )

ServletInvocableHandlerMethod

์œ„ HandlerMethodReturnValueHandlerComposite ํด๋ž˜์Šค๋Š” List์— ๋“ฑ๋ก๋˜์–ด ์žˆ๋Š” Handler ์ค‘์— ์œ„์ž„ํ•˜์—ฌ ๋ฉ”์„œ๋“œ ๋ฐ˜ํ™˜๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” ์ปดํฌ์ง€ํŠธ ํŒจํ„ด์„ ์ ์šฉํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

HandlerMethodReturnValueHandler: handler method ์‹คํ–‰ ํ›„ ๋ฐ˜ํ™˜ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ „๋žต ์ธํ„ฐํŽ˜์ด์Šค
 - supportsReturnType() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์ฃผ์–ด์ง„ ๋ฉ”์„œ๋“œ์˜ ๋ฆฌํ„ด ํƒ€์ž…์ด ์ฒ˜๋ฆฌ๊ฐ€ ๊ฐ€๋Šฅํ•œ์ง€ ํŒ๋ณ„
 - handleReturnValue() ๋ฉ”์„œ๋“œ์—์„œ ์ฃผ์–ด์ง„ ๋ฐ˜ํ™˜ ๊ฐ’์„ ํ† ๋Œ€๋กœ ์ฒ˜๋ฆฌ

HandlerAdapter๊ฐ€ Controller์—๊ฒŒ ์š”์ฒญ์ด ๋๋‚˜๋ฉด ๋‹ค์Œ์—๋Š” ์‘๋‹ต์— ๋Œ€ํ•œ ํ›„์ฒ˜๋ฆฌ๋กœ returnValueHandlers๋ฅผ ํ†ตํ•ด ์ฒ˜๋ฆฌ๋ฉ๋‹ˆ๋‹ค.

API์˜ return type์€ String์ธ๋ฐ์š”, ์—ฌ๊ธฐ์— ๋งž๋Š” returnValueHandlers๋Š” RequestResponseBodyMethodProcessor ์ž…๋‹ˆ๋‹ค.

(์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ResponseEntity๊ฐ€ ๋ฆฌํ„ด ํƒ€์ž…์ธ ๊ฒฝ์šฐ HttpEntityMethodProcessor์„ Handler๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.)

 

๋˜ํ•œ @RestController ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— ModelAndView๊ฐ€ ์•„๋‹Œ String ๋ฌธ์ž์—ด์ด ๊ทธ๋Œ€๋กœ ์‘๋‹ต ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜์ด ๋ฉ๋‹ˆ๋‹ค.

 

RequestResponseBodyMethodProcessor ๋‚ด๋ถ€์—์„œ๋Š” input, output Message๋ฅผ ์ƒ์„ฑํ•˜์—ฌ  AbstractMessageConverterMethodProcessor ์ถ”์ƒ ํด๋ž˜์Šค์˜ writeWithMessageConverters() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ , MediaType์„ ๊ฒ€์‚ฌํ•˜์—ฌ ์ ์ ˆํ•œ HttpMessageConverter๋ฅผ ์ฐพ์•„ ์‘๋‹ต์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

โœ๏ธ ์ •๋ฆฌ

์ด์ƒ์œผ๋กœ Spring์—์„œ DispatcherServlet์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์•˜์Šต๋‹ˆ๋‹ค.

 

DispatcherServlet์ด๋ผ๋Š” ํ•˜๋‚˜์˜ ๊ฐœ๋…๋งŒ ๋ณด์•„๋„ ๋ฌด์ˆ˜ํžˆ ๋งŽ์€ ๋กœ์ง๊ณผ ์—ฌ๋Ÿฌ ํŒจํ„ด๋“ค, Spring์˜ ์ฒ ํ•™ ๋“ฑ์„ ๋ณผ ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์•˜์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ ๋””์ž์ธ ํŒจํ„ด์ด ์‹ค์ œ ์ฝ”๋“œ ์†์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์˜ˆ์‹œ๋ฅผ ๋ณด๋‹ˆ ๊ธฐ์กด์— ๊ฐœ๋…๋งŒ ๋ณด๋˜ ๊ฒƒ๊ณผ๋Š” ๋‹ฌ๋ฆฌ ์ดํ•ด๊ฐ€ ๋” ์ž˜ ๋˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

(์‚ฌ์‹ค DispatcherServlet ๋‚ด์—์„œ๋„ ํ•ต์‹ฌ ๊ธฐ๋Šฅ๋งŒ ๋ดค๊ธฐ ๋•Œ๋ฌธ์— ๋ณด์ง€ ๋ชปํ•œ ์ฝ”๋“œ๋„ ๊ต‰์žฅํžˆ ๋งŽ์Šต๋‹ˆ๋‹ค...)

 

์Šคํ”„๋ง์€ ๋งค์šฐ ์˜ค๋ž˜์ „์— ๋งŒ๋“ค์–ด์ง„ ํ”„๋ ˆ์ž„์›Œํฌ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์•„์ง๊นŒ์ง€ ์•„๋ฌด ๋ฌธ์ œ ์—†์ด ๋งŽ์ด ์ž˜ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์„ ๋ณด๋ฉด... ์ •๋ง ๋Œ€๋‹จํ•˜๋„ค์š”.

์™„๋ฒฝํ•œ ์„ค๊ณ„์™€ ์•„ํ‚คํ…์ฒ˜๋Š” ์—†๊ฒ ์ง€๋งŒ, ์†Œํ”„ํŠธ์›จ์–ด์— ์žˆ์–ด์„œ ๊ต‰์žฅํžˆ ์ค‘์š”ํ•œ ์š”์†Œ๋ผ๊ณ  ๋‹ค์‹œ๋” ๋Š๋‚€ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

๋‹ค์Œ ์‹œ๊ฐ„์—๋Š” ์•„์ฃผ ๊ฐ„๋‹จํ•œ DispatcherServlet์„ ์ง์ ‘ ๋งŒ๋“ค์–ด๋ณด๋ฉฐ ์กฐ๊ธˆ ๋” ์ดํ•ด๋„๋ฅผ ๋†’์—ฌ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 

๐Ÿ“„ ์ฐธ๊ณ  ๋ฌธ์„œ

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€