๐Ÿƒ Spring Basic

๐Ÿƒ Spring Basic

๐Ÿ“ข DispatcherServlet

๋ธŒ๋ผ์šฐ์ €์—์„œ ๋ณด๋‚ด๋Š” ์š”์ฒญ์„ ๊ฐ€์žฅ ๋จผ์ € ๋ฐ›๋Š” Front Controller๋กœ์จ, request๋ฐ›์€ ์š”์ฒญ์„ HandlerMapping์—๊ฒŒ ์งˆ์˜ํ•œ๋‹ค.

๐Ÿ’จ DispatcherServlet ๋™์ž‘ ๋ฐฉ์‹

99F8E4335A06F70930

  • HandlerMapping
    • Map ๊ตฌ์กฐ ์ฒ˜๋Ÿผ key(url) : value(method name or Parameter)๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋Š” ํ˜•ํƒœ์ด๋‹ค.
  1. DispatcherServlet์ด HandlerMapping์—๊ฒŒ ์งˆ์˜ํ•œ ๊ฒฐ๊ณผ ์ฐพ์•„๋‚ธ URL์ด Controller์—๊ฒŒ ์ „๋‹ฌ๋˜์–ด
  2. ์ „๋‹ฌ๋œ Parameter์— ํ•ด๋‹นํ•˜๋Š” ๋ฉ”์„œ๋“œ์˜ ์‹คํ–‰๊ณผ ํ•จ๊ป˜
  3. Service๊ณ„์ธต์— ์ •์˜๋˜์–ด์žˆ๋Š” ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์ด ์‹คํ–‰๋œ๋‹ค.
  4. ์ดํ›„ ์‹คํ–‰ํ•œ ๋ฉ”์„œ๋“œ์˜ ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์˜ ๊ฒฐ๊ณผ๋กœ ๋ฆฌํ„ด๋œ Model๊ฐ์ฒด์™€ ๋ทฐ ์ด๋ฆ„๊ณผ ํ•จ๊ป˜ ๋ฐ˜ํ™˜๋˜๊ณ 
  5. ViewResolver๊ฐ€ ์‘๋‹ตํ•  View๋ฅผ ์ฐพ์•„๋‚ด
  6. View๊ฐ์ฒด์— ์š”์ฒญ์„ ์ „๋‹ฌํ•˜๋ฉฐ ์ตœ์ข…๊ฒฐ๊ณผ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

๐Ÿ”– ์ฃผ์š” Annotation

@ResponseBody

Annotation์ด ์œ„์น˜ํ•œ ๋ฉ”์„œ๋“œ์— View๋ฅผ ์ถœ๋ ฅํ•˜๋Š”๊ฒƒ์ด ์•„๋‹Œ return๋˜๋Š” ์ž๋ฃŒํ˜•์ด MessageConverter์— ์˜ํ•ด ๋ณ€ํ™˜๋˜์–ด HTTP Response Body์— ์“ฐ์—ฌ์ ธ ์ „์†ก๋œ๋‹ค.

Controller์—์„œ View๊ฐ€ ์•„๋‹Œ ๊ฐ์ฒด๋ฅผ ๋ฆฌํ„ดํ•ด์•ผ ํ•  ์ƒํ™ฉ์—์„œ (Ajax๊ฐ™์€) ๋ฉ”์„œ๋“œ์˜ ๋ฆฌํ„ด ์ž๋ฃŒํ˜•์„ ์ง€์ • ํ• ๋•Œ ์“ฐ์ธ๋‹ค.

@RequestMapping

๊ธฐ๋ณธ์ ์œผ๋กœ @RequestMapping(value="parameter”, method.RequestMethod.(method_name))์˜ ํ˜•ํƒœ๋ฅผ ๋„๋Š” Annotation์ด๋‹ค. method๋ฅผ ์ง€์ •ํ•  ๊ฒฝ์šฐ valueํ”„๋กœํผํ‹ฐ ํ˜•์‹์€ ํ•„์ˆ˜์  ์ด๋‹ค. ๋ณต์ˆ˜์˜ urlMapping์„ ์œ„ํ•ด์„œ๋Š” value={“parameter1”, “parameter2”}๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค.

@RequestParam

HttpServleRequest๊ฐ์ฒด์˜ getParamter()์™€ ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•œ๋‹ค ํŠน์ • @RequestMapping๋กœ ์‹คํ–‰๋œ ๋ฉ”์„œ๋“œ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์œ ํ˜•์„ /url?key=value์ฒ˜๋Ÿผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

@Autowired

web.xml -> <context-param> -> <context-annotation-config/> ๋ฅผ ํ†ตํ•ด Autowired Annotation์„ ์‚ฌ์šฉํ•œ ํด๋ž˜์Šค์˜ ์˜์กด์„ฑ์„ ๋‚ฎ์ถฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค

@PathVariable

@RequestParam์˜ value๊ฐ’์„ {param}์œผ๋กœ ์„ค์ •ํ–ˆ์„๋•Œ์˜ ๊ฐ’์„ @PathVariable๋กœ ๋ฐ›์•„ ๋ฉ”์„œ๋“œ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์„ ์–ธํ•œ๋‹ค

@ModelAndView

๋ทฐ์—์„œ Controller๋กœ์˜ ๊ฐ์ฒด ์ „๋‹ฌ์ด ์ด๋ฃจ์–ด์ ธ์•ผ ํ• ๋•Œ ๋ฉ”์„œ๋“œ์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

๐ŸŽงListener

ContextLoaderListener๋ฅผ ์ƒ์†๋ฐ›์•„ contextIntialized ํ•จ์ˆ˜์— ๋ฃจํŠธ ์ปจํ…Œ์ด๋„ˆ๋ฅผ web.xml์— ์ž…๋ ฅํ•œ ๊ฒฝ๋กœ์˜ ์„ค์ •์„ ์ด์šฉํ•ด ๋นŒ๋“œํ•˜๋„๋กํ•˜๋Š” ์—ญํ• 

(์˜ˆ์ œ์—์„œ๋Š” /WEB-INF/applicationContext.xml๋ฅผ ํ† ๋Œ€๋กœ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋นŒ๋“œํ•˜๊ณ  ๋ชจ๋“  ์„œ๋ธ”๋ฆฟ์ด ๊ณต์œ ํ•˜๋„๋ก ํ–ˆ๋‹ค.)

๐Ÿ›๏ธ Layered Architecture

enter image description here

๐Ÿ“Œ Service

์Šคํ”„๋ง์—์„œ์˜ ๋น„์ฆˆ๋‹ˆ์Šค ์˜์—ญ์„ ์ผ๋ฐ˜์ ์œผ๋กœ ์„œ๋น„์Šค๋ผ๊ณ  ํ•œ๋‹ค. ๋น„์ฆˆ๋‹ˆ์Šค ๊ณ„์ธต์€ ์ปจํŠธ๋กค๋Ÿฌ์™€ DAO์‚ฌ์ด์˜ ์ค‘๋ณต์ฝ”๋“œ์™€ ๊ฐ์ฒด์ง€ํ–ฅ์˜ ๋ชฉ์ ์— ๋งž๋Š” ์˜์กด์„ฑ์„ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•˜๋Š” ๊ณ„์ธต์ด๋‹ค.

- DAO์˜ ๋ฉ”์„œ๋“œ ๋‹จ์œ„๊ฐ€ ํŠธ๋žœ์žญ์…˜ ๋‹จ์œ„๊ฐ€ ๋œ๋‹ค.
- DAO์˜ CRUD๊ธฐ๋Šฅ ์ด์™ธ์— ํŽ˜์ด์ง• ๊ตฌํ˜„์‹œ Service๊ณ„์ธต์„ ์ด์šฉํ•˜๋Š”๊ฒƒ์ด ๋Œ€ํ‘œ์ ์ด๋‹ค.
- applicationContext.xml์—์„œ ํŠน์ • ํŒจํ‚ค์ง€๊ฐ€ @Service๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์„ค์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
- ERD๋ฅผ ์ด์šฉํ•œ ๋ฐฉ์‹๊ณผ ์œ ์ € ์Šคํ† ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์„ค๊ณ„ํ•˜๊ณ  `Service๊ณ„์ธต์„` ๊ตฌํ˜„ํ•œ๋‹ค.