[Spring] Spring MVC

Goghยท2023๋…„ 1์›” 2์ผ
0

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
9/23

๐ŸŽฏ ๋ชฉํ‘œ : Spring MVC์˜ ์ดํ•ด์™€ ๋™์ž‘๋ฐฉ์‹, ๊ตฌ์„ฑ์š”์†Œ ํ•™์Šต

๐Ÿ“’ Spring MVC

๐Ÿ“Œ Spring MVC?

  • Spring ๋ชจ๋“ˆ์ค‘ Servlet API ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ํด๋ผ์ด ์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ชจ๋“ˆ์„
    spring-webmvc๋ผ๊ณ  ํ•œ๋‹ค.
  • Spring MVC๋Š” ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•ด์ฃผ๋Š” ํ”„๋ ˆ์ž„์›์ด๋‹ค.
  • Model
    • ํด๋ผ์ด์–ธํŠธ์—๊ฒŒ ์‘๋‹ต์œผ๋กœ ๋Œ๋ ค์ฃผ๋Š” ์ž‘์—…์˜ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ ๋ฐ์ดํ„ฐ.
    • ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๊ตฌ์ฒด์ ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๋Š” ์˜์—ญ์„ ์„œ๋น„์Šค ๊ณ„์ธต์ด๋ผํ•œ๋‹ค.
    • ์‹ค์ œ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ๋กœ ๊ตฌํ˜„ํ•œ ๊ฒƒ์„ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด๋ผ ํ•œ๋‹ค.
  • View
    • Model ๋ฐ์ดํ„ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ํ™”๋ฉด์— ๋ณด์—ฌ์ง€๋Š” ๋ฆฌ์†Œ์Šค๋ฅผ ์ œ๊ณตํ•˜๋Š” ์—ญํ• ์„ํ•œ๋‹ค.
    • View์˜ ํ˜•ํƒœ
      • HTML ํŽ˜์ด์ง€ ์ถœ๋ ฅ(Thymeleaf, FreeMarker, JSP+JSTL...)
      • PDF, EXCEL ๋“ฑ์˜ ๋ฌธ์„œ ํ˜•ํƒœ ์ถœ๋ ฅ
      • XML, JSON ๋“ฑ์˜ ํŠน์ • ํ˜•์‹ ํฌ๋งท์œผ๋กœ ๋ณ€ํ™˜.
        • Model ๋ฐ์ดํ„ฐ๋ฅผ ํŠน์ • ํ”„๋กœํ† ์ฝœ ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•œ๋‹ค.
        • ํ”„๋ก ํŠธ ์—”๋“œ์™€ ๋ฐฑ์—”๋“œ ์˜์—ญ์ด ๋ช…ํ™•ํ•˜๊ฒŒ ๊ตฌ๋ถ„๋˜์–ด ๊ฐœ๋ฐœ ๋ฐ ์œ ์ง€๋ณด์ˆ˜๊ฐ€ ์ƒ๋Œ€์ ์œผ๋กœ ์šฉ์ดํ•˜๋‹ค.
  • Controller
    • ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ง์ ‘์ ์œผ๋กœ ์ „๋‹ฌ ๋ฐ›๋Š” ์—”๋“œํฌ์ธํŠธ๋กœ์จ Model๊ณผ View ์ค‘๊ฐ„์—์„œ ์ƒํ˜ธ์ž‘์šฉ์„ ํ•ด์ฃผ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.
    • ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ๊ฑฐ์นœ Model ๋ฐ์ดํ„ฐ๋ฅผ View๋กœ ์ „๋‹ฌํ•˜๋Š” ์—ญํ• .
    • ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด, @GetMapping์„ ํ†ตํ•ด ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ๋ฐ›๋Š”๋‹ค.
    • MemberService์˜ .findMember()๋ฅผ ํ˜ธ์ถœํ•ด Service๋‹จ์˜ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•œ๋‹ค.
@RestController
@RequestMapping("/members")
public class MemberController {
    private final MemberService memberService;

  MemberController(MemberService memberService) {
        this.memberService = memberService;
    }

    @GetMapping("/{member-id}")  // (1)
    public Member getMember(@PathVariable("member-id") long memberId) {
        return memberService.findMember(memberId); // (2)
    }
}

๐Ÿ“Œ Spring MVC์˜ ๋™์ž‘ ๋ฐฉ์‹๊ณผ ๊ตฌ์กฐ

a

  • ์œ„ ๊ทธ๋ฆผ์œผ๋กœ Spring MVC๊ฐ€ ๋‚ด๋ถ€์ ์œผ๋กœ ์š”์ฒญ์„ ์–ด๋–ป๊ฒŒ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ํ™•์ธ ํ• ์ˆ˜ ์žˆ๋‹ค.
    1. ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์€ DispatcherServlet์œผ๋กœ ์ „๋‹ฌ๋œ๋‹ค.
    2. DispatcherServlet์€ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  Controller์— ๋Œ€ํ•œ ๊ฒ€์ƒ‰์„ HandlerMapping ์ธํ„ฐํŽ˜์ด์Šค์— ์š”์ฒญํ•œ๋‹ค.
    - HandlerMapping์€ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ๊ณผ ๋งคํ•‘๋˜๋Š” ํ•ธ๋“ค๋Ÿฌ ๊ฐ์ฒด๋ฅผ ๋‹ค์‹œ DispatcherServlet์—๊ฒŒ ๋ฐ˜ํ™˜ํ•ด์ค€๋‹ค.
    3. ์‹ค์ œ ํด๋ผ์ด ์–ธํŠธ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•  Controller ํด๋ž˜์Šค์˜ Handler ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐพ์•„ ํ˜ธ์ถœํ•˜๊ธฐ ์œ„ํ•ด DispatcherServlet์€
    Handler ๋ฉ”์†Œ๋“œ๋ฅผ ์ง์ ‘ ํ˜ธ์ถœ ํ•˜์ง€ ์•Š๊ณ , HandlerAdapter์— ํ˜ธ์ถœ์„ ์œ„์ž„ํ•œ๋‹ค.
    4. HandlerAdapter๋Š” DispatcherServlet์œผ๋กœ ์ „๋‹ฌ ๋ฐ›์€ Controller ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ Handler ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
    - Controller์˜ Handler ๋ฉ”์†Œ๋“œ๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง ์ฒ˜๋ฆฌํ›„ ๋ฐ˜ํ™˜ ๋ฐ›์€ Model ๋ฐ์ดํ„ฐ๋ฅผ HandlerAdapter์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.
    5. HandlerAdapter๋Š” ์ „๋‹ฌ๋ฐ›์€ Model ๋ฐ์ดํ„ฐ์™€ View ์ •๋ณด๋ฅผ ๋‹ค์‹œ DispatcherServlet์—๊ฒŒ ์ „๋‹ฌํ•œ๋‹ค.
    6. DispatcherServlet์€ ์ „๋‹ฌ ๋ฐ›์€ ๋ฐ์ดํ„ฐ์™€ View ์ •๋ณด๋ฅผ ViewResolver์—๊ฒŒ ์ „๋‹ฌํ•ด์„œ View ๊ฒ€์ƒ‰ ์š”์ฒญํ•œ๋‹ค.
    7. ViewResolver๋Š” View ์ •๋ณด๋ฅผ ์ฐพ์•„์„œ ๋‹ค์‹œ ๋ฐ˜ํ™˜ ํ•ด์ฃผ๋ฉฐ DispatcherServlet์€ View ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด Model ๋ฐ์ดํ„ฐ๋ฅผ ๋„˜๊ฒจ์ฃผ๋ฉด์„œ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ์š”์ฒญํ•œ๋‹ค
    - View๋Š” ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•ด DispatcherServlet์— ์ „๋‹ฌํ•œ๋‹ค.
    8. ์‹ค์ œ๋กœ๋Š” DispatcherServlet์ด View๋กœ ์ „๋‹ฌ ๋ฐ›์€ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ํด๋ผ์ด์–ธํŠธ์— ์ „๋‹ฌํ•˜๊ฒŒ ๋œ๋‹ค.

  • DispatcherServlet์ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๊ฐ€์žฅ ์•ž์— ๋ฐฐ์น˜๋˜์–ด ๋‹ค๋ฅธ ๊ตฌ์„ฑ ์š”์†Œ๋“ค๊ณผ ์ƒํ˜ธ์ž‘์šฉํ•˜๋ฉฐ ํด๋ผ์ด์–ธํŠธ์˜ ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ํŒจํ„ด์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.
    ์ด๋ฅผ Front Controller Pattern ์ด๋ผ๊ณ  ํ•œ๋‹ค.

profile
์ปดํ“จํ„ฐ๊ฐ€ ํ• ์ผ์€ ์ปดํ“จํ„ฐ๊ฐ€

0๊ฐœ์˜ ๋Œ“๊ธ€