Skip to content

Latest commit

ย 

History

History
58 lines (47 loc) ยท 4.74 KB

README.md

File metadata and controls

58 lines (47 loc) ยท 4.74 KB

Java5์— ์ถ”๊ฐ€๋œ ๋ฌธ๋ฒ• ์š”์†Œ๋กœ, ์ž๋ฐ” ์ฝ”๋“œ์— @๋ฅผ ์ด์šฉํ•ด ์ฃผ์„์ฒ˜๋Ÿผ ๋‹ฌ์•„ ํŠน์ˆ˜ํ•œ ์˜๋ฏธ๋ฅผ ๋ถ€์—ฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
ํ”„๋กœ๊ทธ๋žจ ์ฝ”๋“œ์˜ ์ผ๋ถ€๊ฐ€ ์•„๋‹Œ ํ”„๋กœ๊ทธ๋žจ์— ๊ด€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜๊ณ , ์ฝ”๋“œ์— ์ •๋ณด๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ •ํ˜•ํ™”๋œ ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค.
์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ๊น”๋”ํ•ด์ง€๊ณ  ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • @ComponentScan: @Service, @Repository, @Controller, @Configuration์ด ๋ถ™์€ ํด๋ž˜์Šค Bean๋“ค์„ ์ฐพ์•„์„œ Context์— bean๋“ฑ๋ก์„ ํ•ด์ฃผ๋Š” Annotation
  • @Component: ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ž‘์„ฑํ•œ Class๋ฅผ Bean์œผ๋กœ ๋“ฑ๋กํ•˜๊ธฐ ์œ„ํ•œ Annotation
  • @Bean: ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ์ œ์–ด๊ฐ€ ๋ถˆ๊ฐ€๋Šฅํ•œ ์™ธ๋ถ€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ฑ์„ Bean์œผ๋กœ ๋งŒ๋“ค๋ คํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋Š” Annotation
  • @Autowired: ์†์„ฑ(field), setter method, constructor(์ƒ์„ฑ์ž)์—์„œ ์‚ฌ์šฉํ•˜๋ฉฐ Type์— ๋”ฐ๋ผ ์•Œ์•„์„œ Bean์„ ์ฃผ์ž…ํ•ด์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • @Inject: @Autowired ์–ด๋…ธํ…Œ์ด์…˜๊ณผ ๋น„์Šทํ•œ ์—ญํ• 
  • @Controller: Spring์˜ Controller๋กœ Spring MVC์—์„œ Controllerํด๋ž˜์Šค์— ์“ฐ์ž…๋‹ˆ๋‹ค.
  • @RestController: Spring์—์„œ Controller ์ค‘ View๋กœ ์‘๋‹ตํ•˜์ง€ ์•Š๋Š”, Controller๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
โœ… @Controller ์™€ @RestController ์˜ ์ฐจ์ด

๐Ÿ’ก @Controller
API์™€ view๋ฅผ ๋™์‹œ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
๋Œ€์‹  API ์„œ๋น„์Šค๋กœ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๋Š” @ResponseBody๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
view(ํ™”๋ฉด) return์ด ์ฃผ๋ชฉ์ ์ž…๋‹ˆ๋‹ค.

๐Ÿ’ก @RestController
view๊ฐ€ ํ•„์š”์—†๋Š” API๋งŒ ์ง€์›ํ•˜๋Š” ์„œ๋น„์Šค์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
Spring 4.0.1๋ถ€ํ„ฐ ์ œ๊ณตํ•˜๋ฉฐ, @RequestMapping ๋ฉ”์„œ๋“œ๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ @ResponseBody ์˜๋ฏธ๋ฅผ ๊ฐ€์ •ํ•ฉ๋‹ˆ๋‹ค.
data(json, xml ๋“ฑ) return์ด ์ฃผ๋ชฉ์ ์ž…๋‹ˆ๋‹ค.
์ฆ‰, @RestController = @Controller + @ResponseBody ์ธ ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • @Service: Service Class์—์„œ ์“ฐ์ด๋Š” ๊ฒƒ์œผ๋กœ, ๋น„์ฆˆ๋‹ˆ์Šค ๋กœ์ง์„ ์ˆ˜ํ–‰ํ•˜๋Š” Class๋ผ๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์šฉ๋„์ž…๋‹ˆ๋‹ค.
  • @Repository: DAO class์—์„œ ์“ฐ์ด๋ฉฐ, DataBase์— ์ ‘๊ทผํ•˜๋Š” method๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” Class์—์„œ ์“ฐ์ž…๋‹ˆ๋‹ค.
  • @CrossOrigin: CORS ๋ณด์•ˆ์ƒ์˜ ๋ฌธ์ œ๋กœ ๋ธŒ๋ผ์šฐ์ €์—์„œ ๋ฆฌ์†Œ์Šค๋ฅผ ํ˜„์žฌ origin์—์„œ ๋‹ค๋ฅธ ๊ณณ์œผ๋กœ์˜ AJAX์š”์ฒญ์„ ๋ฐฉ์ง€ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.
  • @RequestBody: ์š”์ฒญ์ด ์˜จ ๋ฐ์ดํ„ฐ(JSON์ด๋‚˜ XMLํ˜•์‹)๋ฅผ ๋ฐ”๋กœ Class๋‚˜ model๋กœ ๋งคํ•‘ํ•˜๊ธฐ ์œ„ํ•œ Annotation์ž…๋‹ˆ๋‹ค.
  • @PathVariable: method parameter ์•ž์— ์‚ฌ์šฉํ•˜๋ฉด์„œ ํ•ด๋‹น URL์—์„œ {ํŠน์ •๊ฐ’}์„ ๋ณ€์ˆ˜๋กœ ๋ฐ›์•„ ์˜ฌ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Lombok Annotation

  • @NoArgsConstructor: ๊ธฐ๋ณธ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  • @AllArgsConstructor: ๋ชจ๋“  ํ•„๋“œ ๊ฐ’์„ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
  • @RequiredArgsConstructor: final์ด๋‚˜ @NonNull์ธ ํ•„๋“œ ๊ฐ’๋งŒ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›๋Š” ์ƒ์„ฑ์ž๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค. final ๊ฐ’์ด ํ• ๋‹น๋˜๋ฉด ๋” ์ด์ƒ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • @Getter: Class ๋‚ด ๋ชจ๋“  ํ•„๋“œ์˜ Getter method๋ฅผ ์ž๋™ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • @Setter: Class ๋‚ด ๋ชจ๋“  ํ•„๋“œ์˜ Setter method๋ฅผ ์ž๋™ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
  • @Builder: ์–ด๋Š ํ•„๋“œ์— ์–ด๋–ค ๊ฐ’์„ ์ฑ„์›Œ์•ผ ํ• ์ง€ ๋ช…ํ™•ํ•˜๊ฒŒ ์ •ํ•˜์—ฌ ์ƒ์„ฑ ์‹œ์ ์— ๊ฐ’์„ ์ฑ„์›Œ์ค๋‹ˆ๋‹ค.
  • @Data: @Getter @Setter @EqualsAndHashCode @AllArgsConstructor์„ ํฌํ•จํ•œ Lombok์—์„œ ์ œ๊ณตํ•˜๋Š” ํ•„๋“œ์™€ ๊ด€๋ จ๋œ ๋ชจ๋“  ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒƒ์œผ๋กœ, ์ „์ฒด์ ์ธ ๋ชจ๋“  ๊ธฐ๋Šฅ ํ—ˆ์šฉ์œผ๋กœ ์œ„ํ—˜ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ๋กœ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š”๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.
๐Ÿ’ก Constructor์™€ Builder์˜ ์ฐจ์ด
์ƒ์„ฑ ์‹œ์ ์— ๊ฐ’์„ ์ฑ„์›Œ์ฃผ๋Š” ์—ญํ• ์€ ๋˜‘๊ฐ™์ง€๋งŒ, Builder๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์–ด๋Š ํ•„๋“œ์— ์–ด๋–ค ๊ฐ’์„ ์ฑ„์›Œ์•ผ ํ• ์ง€ ๋ช…ํ™•ํ•˜๊ฒŒ ์ธ์ง€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
ํ•ด๋‹น Class์˜ Builder ํŒจํ„ด Class๋ฅผ ์ƒ์„ฑ ํ›„ ์ƒ์„ฑ์ž ์ƒ๋‹จ์— ์„ ์–ธ ์‹œ ์ƒ์„ฑ์ž์— ํฌํ•จ๋œ ํ•„๋“œ๋งŒ ๋นŒ๋”์— ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

JPA Annotation

  • @Entity: ์‹ค์ œ DB์˜ ํ…Œ์ด๋ธ”๊ณผ ๋งค์นญ๋  Class์ž„์„ ๋ช…์‹œํ•˜๋Š” ๊ฒƒ์œผ๋กœ ํ…Œ์ด๋ธ”๊ณผ ๋งํฌ๋  ํด๋ž˜์Šค์ž„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • @Table: Entity Class์— ๋งคํ•‘ํ•  ํ…Œ์ด๋ธ” ์ •๋ณด๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค. @Table(name = "USER")์™€ ๊ฐ™์€ ํ˜•ํƒœ๋กœ ์‚ฌ์šฉ๋˜๋ฉฐ, Annotation์„ ์ƒ๋žตํ•˜๋ฉด Class ์ด๋ฆ„์„ ํ…Œ์ด๋ธ” ์ด๋ฆ„ ์ •๋ณด๋กœ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.
  • @Id: ํ•ด๋‹น ํ…Œ์ด๋ธ”์˜ PK ํ•„๋“œ๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.
  • @GeneratedValue: PK์˜ ์ƒ์„ฑ ๊ทœ์น™์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ๊ฐ€๋Šฅํ•œ Entity์˜ PK๋Š” Long ํƒ€์ž…์˜ Auto_increment๋ฅผ ์ถ”์ฒœํ•˜๋ฉฐ, ์Šคํ”„๋ง ๋ถ€ํŠธ 2.0์—์„  ์˜ต์…˜์„ ์ถ”๊ฐ€ํ•ด์•ผ auto_increment์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ AUTO๋กœ, MySQL์˜ auto_increment์™€ ๊ฐ™์ด ์ž๋™ ์ฆ๊ฐ€ํ•˜๋Š” ์ •์ˆ˜ํ˜• ๊ฐ’์ด ๋ฉใ„ด๋””ใ….
  • @Column: ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๊ตณ์ด ์„ ์–ธํ•˜์ง€ ์•Š๋”๋ผ๋„ ํ•ด๋‹น Class์˜ ํ•„๋“œ๋Š” ๋ชจ๋‘ ์ปฌ๋Ÿผ์ด ๋ฉ๋‹ˆ๋‹ค. @Column์„ ์ƒ๋žตํ•˜๋ฉด ํ•„๋“œ๋ช…์„ ์‚ฌ์šฉํ•ด์„œ ์ปฌ๋Ÿผ๋ช…๊ณผ ๋งคํ•‘ํ•ฉ๋‹ˆ๋‹ค.