๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์ด๋ก /Spring

[Spring Boot] Spring MVC ๊ตฌ์กฐ / ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘๊ณผ ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ

by 6161990 2021. 8. 8.

๐Ÿ“Œ DispatcherServlet ์„œ๋ธ”๋ฆฟ 

  • DispatcherServlet ๋„ ๋ถ€๋ชจ ํด๋ž˜์Šค์—์„œ HttpServlet์„ ์ƒ์† ๋ฐ›์•„์„œ ์‚ฌ์šฉํ•˜๊ณ , ์„œ๋ธ”๋ฆฟ์œผ๋กœ ๋™์ž‘ํ•œ๋‹ค.
    • DispatcherServlet -> FrameworkServlet -> HttpServletBean -> HttpServlet
  • ์Šคํ”„๋ง ๋ถ€ํŠธ๋Š” DispatcherServlet์„ ์„œ๋ธ”๋ฆฟ์œผ๋กœ ์ž๋™์œผ๋กœ ๋“ฑ๋กํ•˜๋ฉด์„œ ๋ชจ๋“  ๊ฒฝ๋กœ(urlPatterns="/")์— ๋Œ€ํ•ด์„œ ๋งคํ•‘ํ•œ๋‹ค.
์ฐธ๊ณ  : ๋” ์ž์„ธํ•œ ๊ฒฝ๋กœ๊ฐ€ ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’๋‹ค. 

 

์š”์ฒญ ํ๋ฆ„

  • ์„œ๋ธ”๋ฆฟ์ด ํ˜ธ์ถœ๋˜๋ฉด HttpServlet์ด ์ œ๊ณตํ•˜๋Š” service()๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค. 
  • ์Šคํ”„๋ง MVC๋Š” DispatcherServlet์˜ ๋ถ€๋ชจ์ธ FrameworkServlet์—์„œ service()๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด๋‘์—ˆ๋‹ค. 
  • FrameworkServlet.service()๋ฅผ ์‹œ์ž‘์œผ๋กœ ์—ฌ๋Ÿฌ ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋ฉด์„œ DispatcherServlet.doDispatch()๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.

 

 

 

 

๋™์ž‘ ์ˆœ์„œ

1. ํ•ธ๋“ค๋Ÿฌ ์กฐํšŒ : ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘์„ ํ†ตํ•ด ์š”์ฒญ URL์— ๋งคํ•‘๋œ ํ•ธ๋“ค๋Ÿฌ(์ปจํŠธ๋กค๋Ÿฌ)๋ฅผ ์กฐํšŒํ•œ๋‹ค.

2. ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ ์กฐํšŒ : ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ๋ฅผ ์กฐํšŒํ•œ๋‹ค.

3. ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ ์‹คํ–‰ : ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

4. ํ•ธ๋“ค๋Ÿฌ ์‹คํ–‰ : ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ๊ฐ€ ์‹ค์ œ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

5. ModelAndView ๋ฐ˜ํ™˜ : ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ๋Š” ํ•ธ๋“ค๋Ÿฌ๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ •๋ณด๋ฅผ ModelAndView๋กœ ๋ณ€ํ™˜ํ•ด์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

6. viewResolver ํ˜ธ์ถœ  : ๋ทฐ ๋ฆฌ์กธ๋ฒ„๋ฅผ ์ฐพ๊ณ  ์‹คํ–‰ํ•œ๋‹ค.

7. View ๋ฐ˜ํ™˜ : ๋ทฐ ๋ฆฌ์กธ๋ฒ„๋ฅผ ๋ทฐ์˜ ๋…ผ๋ฆฌ ์ด๋ฆ„์„ ๋ฌผ๋ฆฌ ์ด๋ฆ„์œผ๋กœ ๋ฐ”๊พธ๊ณ , ๋žœ๋”๋ง ์—ญํ• ์„ ๋‹ด๋‹นํ•˜๋Š” ๋ทฐ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

8. ๋ทฐ ๋ Œ๋”๋ง : ๋ทฐ๋ฅผ ํ†ตํ•ด์„œ ๋ทฐ๋ฅผ ๋ Œ๋”๋ง ํ•œ๋‹ค. 

 

 

์ธํ„ฐํŽ˜์ด์Šค ์‚ดํŽด๋ณด๊ธฐ 

  • ์Šคํ”„๋ง MVC์˜ ํฐ ๊ฐ•์ ์€ DispatcherServlet ์ฝ”๋“œ์˜ ๋ณ€๊ฒฝ ์—†์ด, ์›ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ์ ์ด๋‹ค.
  • ์ฃผ์š” ์ธํ„ฐํŽ˜์ด์Šค ๋ชฉ๋ก
    • ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘ : org.springframework.web.servlet.HandlerMapping
    • ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ : org.springframework.web.servlet.HandlerAdapter
    • ๋ทฐ ๋ฆฌ์กธ๋ฒ„ : org.springframework.web.servlet.ViewResolver
    • ๋ทฐ : org.springframework.web.servlet.View

 

 


 

 

๐Ÿ“Œ ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘๊ณผ ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ 

 

Controller ์ธํ„ฐํŽ˜์ด์Šค

๊ณผ๊ฑฐ ๋ฒ„์ „ ์Šคํ”„๋ง ์ปจํŠธ๋กค๋Ÿฌ org.springframework.web.servlet.mvc.Controller

public interface Controller {

     ModelAndView handlerRequest (HttpServletRequest request, HttpServletResponse response) throws Exception; 

}

 

 

 

๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด๋ณด๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

 

  • @Component : ์ด ์ปจํŠธ๋กค๋Ÿฌ๋Š” /springmvc/old-controller ๋ผ๋Š” ์ด๋ฆ„์˜ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋ก๋˜์—ˆ๋‹ค. 
  • ๋นˆ์˜ ์ด๋ฆ„์œผ๋กœ URL ๋งคํ•‘ํ•  ๊ฒƒ์ด๋‹ค. 

ํ•ด๋‹น ์ปจํŠธ๋กค๋Ÿฌ๋Š” ์–ด๋–ค ๋ฐฉ์‹์œผ๋กœ ํ˜ธ์ถœ๋˜๋Š”๊ฑธ๊นŒ?

 

 

์ด ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ํ˜ธ์ถœ๋˜๋ ค๋ฉด ๋‹ค์Œ 2๊ฐ€์ง€๊ฐ€ ํ”ผ๋ฃกํ•˜๋‹ค.

 

  • HandlerMapping(ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘)
    • ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘์—์„œ ์ด ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์–ด์•ผํ•œ๋‹ค.
    • ์˜ˆ ) ์Šคํ”„๋ง ๋นˆ์˜ ์ด๋ฆ„์œผ๋กœ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋Š” ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘์ด ํ•„์š”ํ•˜๋‹ค.
  • HandlerAdapter(ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ)
    • ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘์„ ํ†ตํ•ด์„œ ์ฐพ์€ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.
    • ์˜ˆ ) Controller ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ๋ฅผ ์ฐพ๊ณ  ์‹คํ–‰ํ•ด์•ผํ•œ๋‹ค. 

์Šคํ”„๋ง์€ ์ด๋ฏธ ํ•„์š”ํ•œ ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘๊ณผ ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ๋ฅผ ๋Œ€๋ถ€๋ถ„ ๊ตฌํ˜„ํ•ด๋‘์—ˆ๋‹ค. ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘๊ณผ ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ์ผ์€ ๊ฑฐ์˜ ์—†๋‹ค.

 

 

์Šคํ”„๋ง๋ถ€ํŠธ๊ฐ€ ์ž๋™ ๋“ฑ๋กํ•˜๋Š” ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘๊ณผ ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ (*์ค‘์š” ๋ถ€๋ถ„ ์œ„์ฃผ๋กœ๋งŒ ์„ค๋ช…)

HandlerMapping

0 = RequestMappingHandlerMapping  : ์• ๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜์˜ ์ปจํŠธ๋กค๋Ÿฌ์ธ @RequestMapping์—์„œ ์‚ฌ์šฉ
1 = BeanNameUrlHandlerMapping : ์Šคํ”„๋ง ๋นˆ์˜ ์ด๋ฆ„์œผ๋กœ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ฐพ๋Š”๋‹ค.

 

 

HandlerAdapter

0  = RequestMappingHandlerAdapter  : ์• ๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜์˜ ์ปจํŠธ๋กค๋Ÿฌ์ธ @RequestMapping์—์„œ ์‚ฌ์šฉ
1  = HttpRequestHandlerAdapter  : HttpRequestHandler ์ฒ˜๋ฆฌ 
2  = SimpleControllerHandlerAdapter : Controller ์ธํ„ฐํŽ˜์ด์Šค (์• ๋…ธํ…Œ์ด์…˜ X, ๊ณผ๊ฑฐ์— ์‚ฌ์šฉ)

 

ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘๋„, ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ๋„ ๋ชจ๋‘ ์ˆœ์„œ๋Œ€๋กœ ์ฐพ๊ณ  ๋งŒ์•ฝ ์—†์œผ๋ฉด ๋‹ค์Œ ์ˆœ์„œ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.

 

 

1. ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘์œผ๋กœ ํ•ธ๋“ค๋Ÿฌ ์กฐํšŒ

1. HandlerMapping ์„ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰ํ•ด์„œ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ฐพ๋Š”๋‹ค.

2. ์ด ๊ฒฝ์šฐ ๋นˆ ์ด๋ฆ„์œผ๋กœ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ฐพ์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ๋นˆ ์ด๋ฆ„์œผ๋กœ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ฐพ์•„์ฃผ๋Š” BeanNameUrlHandlerMapping ๊ฐ€ ์‹คํ–‰์— ์„ฑ๊ณตํ•˜๊ณ  ํ•ธ๋“ค๋Ÿฌ์ธ OldController๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

2. ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ ์กฐํšŒ

1. HandlerAdapter์˜ supports()๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํ˜ธ์ถœํ•œ๋‹ค.

2. SimpleControllerHandlerAdapter ๊ฐ€ Controller ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ง€์›ํ•˜๋ฏ€๋กœ ๋Œ€์ƒ์ด๋œ๋‹ค. 

 

3. ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ ์‹คํ–‰

1. ๋””์ŠคํŒจ์ฒ˜ ์„œ๋ธ”๋ฆฟ์ด ์กฐํšŒํ•œ SimpleControllerHandlerAdapter๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ํ•ธ๋“ค๋Ÿฌ ์ •๋ณด๋„ ํ•จ๊ป˜ ๋„˜๊ฒจ์ค€๋‹ค.

2. SimpleControllerHandlerAdapter ๋Š” ํ•ธ๋“ค๋Ÿฌ์ธ OldController๋ฅผ ๋‚ด๋ถ€์—์„œ ์‹คํ–‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

์ •๋ฆฌ OldController ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘, ์–ด๋Œ‘ํ„ฐ

OldController๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ์‚ฌ์šฉ๋œ ๊ฐ์ฒด๋Š” ๋‹ค์Œ๊ณผ๊ฐ™๋‹ค.

  • HandlerMapping = BeanNameUrlHandlerMapping 
  • HandlerAdapter = SimpleControllerHandlerAdapter 

 

๊ทธ๋ ‡๋‹ค๋ฉด !!

์ €๊ธฐ์„œ HttpRequestHandler๋Š” ๋ญ˜๊นŒ?

๊ฒฐ๋ก ๋ถ€ํ„ฐ ๋งํ•˜๋ฉด HttpRequestHandler ๋Š” Controller์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ํ•ธ๋“ค๋Ÿฌ์ธ๋ฐ, ์„œ๋ธ”๋ฆฟ๊ณผ ๊ฐ€์žฅ ์œ ์‚ฌํ•œ ํ˜•ํƒœ์˜ ํ•ธ๋“ค๋Ÿฌ ์ด๋‹ค. 

 

public interface HttpRequestHandler {

     void handlerRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException; 

}

 

๊ฐ„๋‹จํ•˜๊ฒŒ ๊ตฌํ˜„ํ•ด๋ณด์ž.

 

1. ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘์œผ๋กœ ํ•ธ๋“ค๋Ÿฌ ์กฐํšŒ

1. HandlerMapping ์„ ์ˆœ์„œ๋Œ€๋กœ ์‹คํ–‰ํ•ด์„œ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ฐพ๋Š”๋‹ค.

2. ์ด ๊ฒฝ์šฐ ๋นˆ ์ด๋ฆ„์œผ๋กœ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ฐพ์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ๋นˆ ์ด๋ฆ„์œผ๋กœ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ฐพ์•„์ฃผ๋Š” BeanNameUrlHandlerMapping ๊ฐ€ ์‹คํ–‰์— ์„ฑ๊ณตํ•˜๊ณ  ํ•ธ๋“ค๋Ÿฌ์ธ MyHttpRequestHandler๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

2. ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ ์กฐํšŒ

1. HandlerAdapter์˜ supports()๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ํ˜ธ์ถœํ•œ๋‹ค.

2. HttpRequestHandlerAdapter ๊ฐ€ Controller ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ง€์›ํ•˜๋ฏ€๋กœ ๋Œ€์ƒ์ด๋œ๋‹ค. 

 

3. ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ ์‹คํ–‰

1. ๋””์ŠคํŒจ์ฒ˜ ์„œ๋ธ”๋ฆฟ์ด ์กฐํšŒํ•œ HttpRequestHandlerAdapter ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ํ•ธ๋“ค๋Ÿฌ ์ •๋ณด๋„ ํ•จ๊ป˜ ๋„˜๊ฒจ์ค€๋‹ค.

2. HttpRequestHandlerAdapter ๋Š” ํ•ธ๋“ค๋Ÿฌ์ธ MyHttpRequestHandler๋ฅผ ๋‚ด๋ถ€์—์„œ ์‹คํ–‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

 

์ •๋ฆฌ MyHttpRequestHandler ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘, ์–ด๋Œ‘ํ„ฐ

OldController๋ฅผ ์‹คํ–‰ํ•˜๋ฉด์„œ ์‚ฌ์šฉ๋œ ๊ฐ์ฒด๋Š” ๋‹ค์Œ๊ณผ๊ฐ™๋‹ค.

  • HandlerMapping = BeanNameUrlHandlerMapping 
  • HandlerAdapter = HttpRequestHandlerAdapter 

 

์ฐธ๊ณ  : @RequestMapping 
๊ฐ€์žฅ ์šฐ์„  ์ˆœ์œ„๊ฐ€ ๋†’์€ ํ•ธ๋“ค๋Ÿฌ ๋งคํ•‘๊ณผ ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ๋Š” 
RequestMappingHanlderMapping
RequestMappingHandlerAdapter์ด๋‹ค. 
@RequestMapping์˜ ์•ž๊ธ€์ž๋ฅผ ๋”ฐ์„œ ๋งŒ๋“  ์ด๋ฆ„์ธ๋ฐ, ์ด๊ฒƒ์ด ๋ฐ”๋กœ ์ง€๊ธˆ ์Šคํ”„๋ง์—์„œ ์ฃผ๋กœ ์‚ฌ์šฉํ•˜๋Š” ์• ๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜์˜ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ง€์›ํ•˜๋Š” ๋งคํ•‘๊ณผ ์–ด๋Œ‘ํ„ฐ์ด๋‹ค. ์‹ค๋ฌด์—์„œ๋Š” ๋Œ€๋ถ€๋ถ„ ์ด ๋ฐฉ์‹์˜ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.