Spring HTTP Server by MVC
Model-View-Controller ์ํคํ
์ฒ ํจํด
MVC ํจํด์ด๋ผ๊ณ ๋ถ๋ฅด๋ ํจํด์ด๋ค.
View๋ GUI์ ๊ด๋ จ๋ ํํ๊ณ์ธต.
Controller๋ ํด๋ผ์ด์ธํธ์์ ๋ค์ด์ค๋ ์ ๋ ฅ์ ๊ดํ ์ฒ๋ฆฌ๋ฅผ ํ๋ ๊ณ์ธต์ด๋ค.
Model์ ๊ทธ ์ธ์ ๋ชจ๋ ์ฒ๋ฆฌ๋ฅผ ํ๋ ๊ณ์ธต์ด๋ค.
๊ตณ์ด M, V, C๋ก ๋ ์ด์ด๋ฅผ ๋๋๋ ์ด์ ๊ฐ ๋ญ๊น? ์ด์ ๋ Model์ ๋ํ 2๊ฐ์ง ์๋ฏธ๊ฐ ์๊ธฐ ๋๋ฌธ์ด๋ค.
๊ด์ฌ์ฌ์ ๋ถ๋ฆฌ์ ์ฐจ์. MVC์์ ์ถ๊ตฌํ๋ ๋ถ๋ถ์ด๋ค.
Model์ ํต์ฌ ๋น์ฆ๋์ค๋ง ๋ค๋ฃฐ ์ ์๋๋ก ํ๊ธฐ ์ํด ๋ถ๋ฆฌํ๋ค.
์์) View๊ณ์ธต์์ ์ฌ์ฉ์๊ฐ ์ด๋ค ๋ฒํผ์ ๋๋ฌ on/off ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ค.
Controller์์๋ ํด๋ผ์ด์ธํธ์์ ๋ค์ด์จ ์ ๋ ฅ์ ํตํด toggle.onOff()๋ผ๋ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ค.
Model๊ณ์ธต์์๋ ๋น์ฆ๋์ค ์ฒ๋ฆฌ๋ฅผ ํ๋ค.
(์ฃผ๋ก ์น์์) View์์ ์ฐธ์กฐํ๋ ๋ฐ์ดํฐ๋ ํํ์ผ๋ก์ Model
MVC๋ฅผ ์ฌ์ฉํ๋ Rails์ ๊ฒฝ์ฐ๋ Active Record์ํคํ ์ณ ํจํด์ด๋ผ๋ ๊ฒ์ ์ฌ์ฉํ๋ค.
Active Record๋ผ๋ ๊ฒ ์์ฒด๊ฐ DBํ ์ด๋ธ ์์ฒด๋ฅผ 1:1๋ก ๋งคํํ์ฌ ์ฌ์ฉํ๋ค.
DB์ ์ง์ ๋ช ๋ น์ ๋ด๋ฆฌ๊ฑฐ๋ ์กฐ์์์ด ์ดํ๋ฆฌ์ผ์ด์ ๋ ๋ฒจ์์ DB๋ฅผ ์กฐ์ํ ์ ์๋ค.
ํ์ง๋ง Active Record๊ฐ ๋ง๋ฅ ์ข์ง๋ง์ ์๋ค. Active Record considered harmfulํ๋ค๋ ์๊ฒฌ๋ ์๋ค๊ณ ํ๋ค. ๋ค์ต์คํธ๋ผ๋์ด goto์ hamfulํจ์ ์ ์ ๊ธ์ ํจ๋ฌ๋ ํ ํ์์ด๋ผ๊ณ ํ๋คใ
Spring Web MVC์์๋ Map๊ณผ ์ ์ฌํ ModelMap์ด๋ผ๋ ํํ๋ฅผ ์ฌ์ฉํ์ฌ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๋ค.
Spring web MVC์์๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ์ฒด์ ๊ฑธ์ณ์ ์ด ๊ฐ๋ ์ ์ฌ์ฉํ์ง ์๊ณ , ํด๋ผ์ด์ธํธ(์น, UI Layer)์ ๊ด๋ จ๋ Layer๊น์ง๋ง ์ด MVC ๊ฐ๋ ์ ์ฌ์ฉํ๋ค. ๊ทธ ๋ฐ์ Domain Layer์์๋ ๊ทธ ๋น์ฆ๋์ค์๋ง ์ง์คํ๋๋ก ๋ถ๋ฆฌํ๋ค.
๊ฒฐ๋ก ์ Model์ด ๊ผญ DB์ ์ฐ๊ฒฐ๋๋ ๊ฐ๋ ์ ์๋๋ผ๋ ๊ฒ์ด๋ค.
์ต๊ทผ์๋ UI Layer๋ฅผ ์์ Front๋ก ๋ถ๋ฆฌํ๊ณ , Backend๋ api๋ง ์ ๊ณตํ๋ ํ์์ ๋ง์ด ์ฌ์ฉํ๋ค. ๊ทธ๋ฌ๋ค๋ณด๋ UI Layer๊ฐ ์ ํ๋ฆฌ์ผ์ด์ ์ ์์ด Controller๋ง์ผ๋ก ์ถฉ๋ถํ ๊ตฌ์ฑํ ์ ์๊ฒ ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋์๊ฐ Spring5๋ถํฐ ์๊ธด WebFlux์ ๊ฒฝ์ฐ Controller๊ฐ ์๋ handler๋ผ๋ ๊ฐ๋ ์ผ๋ก ์ ๊ทผํ ์๋ ์๋ค. Hexagonal ์ํคํ ์ณ์ ๊ฒฝ์ฐ์๋ Adapter๋ฅผ ์ฌ์ฉํ๋ค.(Hexagonal์ ๋์ค์...)
package com.example.app.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WelcomController {
@GetMapping("/")
public String home() {
return "Hello world.";
}
@GetMapping("/hi")
public String hi() {
return "Hi, world.";
}
}
Annotation ๊ธฐ๋ก
Last updated