๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๊ฐœ๋ฐœ/TIL

[Spring] Spring Boot Basics

by candosh 2023. 7. 10.

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป JVM - JRE - JDK

JVM

: Java Virtual Machine
 Java ByteCode๋ฅผ ๊ธฐ๊ณ„์–ด๋กœ ๋ฐ”๊พธ์–ด์ฃผ๋Š” ์—ญํ• ์„ ํ•จ.

JRE

: Java Runtime Environment
JRE์—๋Š” JVM๊ณผ JVM์ด ์‚ฌ์šฉํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋“ฑ์„ ํฌํ•จ

JDK

: Java Development Kit
Java ์ฝ”๋“œ๋ฅผ Java Bytecode๋กœ ๋งŒ๋“œ๋Š” ๊ณผ์ •(์ปดํŒŒ์ผ) ๋“ค์„ ํ•ด์ฃผ๋Š” ๋„๊ตฌ๋ฅผ ํฌํ•จํ•œ ๊ฒƒ
Java ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•œ ํ”„๋กœ๊ทธ๋žจ

**Java Bytecode๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ ๊ผญ Java๋ฅผ ์“ธ ํ•„์š”๋Š” ์—†๋‹ค. (ex. Kotlin)

๋”๋ณด๊ธฐ

Mac - JDK ํ„ฐ๋ฏธ๋„๋กœ ๋ฒ„์ „ ๋ณ€๊ฒฝํ•˜๊ธฐ

https://ifuwanna.tistory.com/247

 

๐Ÿณ Docker

์ด์ „ ๊ฐ€์ƒํ™” ๊ธฐ์ˆ ์— ๋น„ํ•˜์—ฌ ํ›จ์”ฌ ๊ฐ€๋ณ๋‹ค.

Docker

:์ปจํ…Œ์ด๋„ˆ ๊ฐœ๋…์„ ํ™œ์šฉํ•œ ๊ฐ€์ƒํ™” ๊ธฐ์ˆ ์˜ ์ผ์ข…
OS ์œ„์— ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์˜ฌ๋ ค ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํ™œ์šฉ

๋‚ด๊ฐ€ ๋งŒ๋‚ฌ๋˜ ์—๋Ÿฌ

๋”๋ณด๊ธฐ

Docker ์„ค์น˜ ํ•˜๋ฉด์„œ ๋งŒ๋‚ฌ๋˜ ์—๋Ÿฌ

:Docker๋ฅผ ๋ถ„๋ช…ํžˆ ๊น”๊ณ , ์‹คํ–‰๊นŒ์ง€ ๋๋Š”๋ฐ ํ„ฐ๋ฏธ๋„์—์„œ Docker ๋ช…๋ น์–ด๋ฅผ ๋ชป ์•Œ์•„๋“ค์—ˆ๋‹ค.

zsh: command not found: docker

 

ํ•ด๊ฒฐ ๋ฐฉ์•ˆ

vi ~/.zshrc

// iํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ํŽธ์ง‘ ๋ชจ๋“œ๊ฐ€ ๋˜์–ด ํŽธ์ง‘์ด ์ง„ํ–‰๋œ๋‹ค. ํŽธ์ง‘์„ ๋งˆ์นœ ํ›„ esc๋ˆ„๋ฅด๊ณ  :wq!์„ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ํŽธ์ ‘๊ธฐ๋ฅผ ๋น ์ ธ๋‚˜์˜ฌ ์ˆ˜ ์žˆ๋‹ค.

alias docker="/Applications/Docker.app/Contents/Resources/bin/docker"

source ~/.zshrc

 

๐Ÿƒ Spring Boot

Spring Boot

(Web) Application FrameWork
HTTP ์š”์ฒญ ๋“ฑ์„ ๋ฐ›์œผ๋ฉด ์š”์ฒญ์— ๋”ฐ๋ฅธ ์‘๋‹ต์„ ๋Œ๋ ค์ฃผ๋Š” Web Application์„ ๋งŒ๋“œ๋Š” FrameWork

**FrameWork : ํŠน์ •ํ•œ ๋ชฉ์ ์„ ์œ„ํ•œ ๋„๊ตฌ ๋˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋ชจ์Œ

 

๐Ÿ› ๏ธ Java์™€ Build Automation Tool

Java๋กœ ์ด๋ฃจ์–ด์ง„ ํ”„๋กœ์ ํŠธ๋Š” '*.java' ํŒŒ์ผ์— ์ •์˜๋œ Java Source ์ฝ”๋“œ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.
Java Compiler๋Š” Java Bytecode ๋ณ€ํ™˜ -> Java Bytecode ์‹คํ–‰ -> ์‹คํ–‰ ๊ฐ€๋Šฅํ•œ ํŒŒ์ผ๋กœ ์ œ์ž‘(ํŒจํ‚ค์ง€) -> ๋„˜ ๋ณต์žก
๊ทธ๋ž˜์„œ Maven ๊ณผ Gradle๋กœ ์‚ฌ์šฉ

Maven

  • Java๋ฅผ ์œ„ํ•œ Build Automation Tool
  • ๋ฏธ๋ฆฌ ์ž‘์„ฑ๋œ xml ํŒŒ์ผ์„ ์ด์šฉํ•˜์—ฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ž๋™์œผ๋กœ ๋‹ค์šด๋กœ๋“œํ•˜๊ฑฐ๋‚˜ ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•ด์ค€๋‹ค. ์ž๋ฐ” ์†Œ์Šค๋ฅผ ์ปดํŒŒ์ผํ•˜๊ณ  ํŒจํ‚ค์ง€ํ•ด์„œ deploy๊นŒ์ง€ ์ž๋™ํ™” ํ•ด์ค€๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
  • Project Object Model (POM)
    • xml์˜ ํ˜•ํƒœ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ •์˜
    • pom.xml์„ ๋ถ„์„ํ•ด ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ
  • <dependency></dependency> ์ค‘์š”
    • ํ”„๋กœ์ ํŠธ๊ฐ€ ์˜์กดํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋“ค์˜ ์ •๋ณด
    • ์•ˆ์— ๋“ค์–ด๊ฐ€๋Š” ๋Œ€ํ‘œ ํƒœ๊ทธ 4๊ฐ€์ง€
    • <groupId> : ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๊ทธ๋ฃน ์•„์ด๋””
    • <artifactId> : ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์•„ํ‹ฐํŒฉํŠธ ์•„์ด๋””
    • <version> : ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ๋ฒ„์ „
    • <scope> : ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ํ”„๋กœ์ ํŠธ ๋‚ด์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋ฒ”์œ„

Gradle

  • Java๋ฅผ ์œ„ํ•œ Build Automation Tool
  • Ant์™€ Maven๊ณผ ๊ฐ™์€ ์ด์ „ ์„ธ๋Œ€ ๋นŒ๋“œ ๋„๊ตฌ์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ณ  ์žฅ์ ์„ ์ทจํ•ฉํ•˜์—ฌ ๋งŒ๋“  ์˜คํ”ˆ์†Œ์Šค๋กœ ๊ณต๊ฐœ๋œ ๋นŒ๋“œ ๋„๊ตฌ์ด๋‹ค.
  • build.gradle
    • ๊ทธ๋ฃจ๋น„(Groovy)๋ผ๋Š” ์–ธ์–ด๋กœ ํ”„๋กœ์ ํŠธ ์ •์˜
    • sub-project ๋“ฑ์„ ํฌํ•จ์‹œํ‚ค๋Š” ์šฉ๋„์˜ setting.gradle๋„ ์žˆ๋‹ค.
    • kotlin์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค. (build.gradle.kts)

 

๐Ÿ’ป ์ผ๋ฐ˜์ ์ธ ์›น ์„œ๋ฒ„์˜ ๋ฐฐํฌ๊ตฌ์กฐ

Hardware -> Web server -> Web Application Server or FileSystem

 

But

  Hardware -> Spring Boot ๊ฐ€๋Šฅ!์ด๋ ‡๊ฒŒ ์„œ๋ฒ„ ๊ตฌ์„ฑ์€ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์ด ๊ฐ€๋Šฅํ•˜๊ณ , Spring Boot ์‚ฌ์šฉ ์‹œ ๊ฐ„๋‹จ!

 

Java์™€ Jar

: Spring Boot ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•˜๋ฉด jar ํŒŒ์ผ์ด ๋‚˜์˜จ๋‹ค.

jar

: Java ARchive

Java๋กœ ์ž‘์„ฑ ํ›„ ์ปดํŒŒ์ผ๋œ Java Bytecode์™€ ์‹คํ–‰์„ ์œ„ํ•ด ํ•„์š”ํ•œ ๋‹ค์–‘ํ•œ ์ž์›์„ ๋ฐฐํฌ๋ฅผ ์œ„ํ•ด์—ฌ ๋ชจ์•„๋†“์€ ํŒŒ์ผ์˜ ํ˜•ํƒœ, ์‰ฝ๊ฒŒ ๋งํ•ด ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ ์••์ถœ ํŒŒ์ผ

 

๐Ÿ™ƒ Web Application์˜ ๊ตฌ์กฐ

Presentation Layout : ์‚ฌ์šฉ์ž์™€ ์ง์ ‘์ ์œผ๋กœ ๋งˆ๋‹ฟ๋Š” ๋ถ€๋ถ„
Logic Layout : ์š”์ฒญ์„ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฒฐ์ •์„ ๋‚ด๋ฆฌ๋Š” ๋ถ€๋ถ„
Data Layout : ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ถ€๋ถ„

 

๐Ÿƒ Spring Boot์˜ ์ž‘์€ ๊ตฌ์กฐ

 

๐Ÿซ‚ Java์˜ Interface

ํ•จ์ˆ˜ ์ธ์ž์™€ ๋ฐ˜ํ™˜ ๊ฐ’์€ interface๋ฅผ ํ™œ์šฉํ•˜์ž
Inputstream์˜ ๊ตฌํ˜„์ฒด๋Š” ๋งŽ์ง€๋งŒ ๋‹ค Inputstream์˜ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค -> Inoputstream์„ ํ•„์š”๋กœ ํ•˜๋Š” ๊ธฐ๋Šฅ์—๋Š” ๊ตฌ๋ถ„์—†์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋œป!

 

๐ŸŒ Spring IoC Container์™€ DI

IoC: Inversion of control (์ œ์–ด ์—ญ์ „)
๊ฐœ๋ฐœ์ž๊ฐ€ Frame Work๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ์ œ์–ด๊ฐ€ ์—ญ์ „ ๋˜์—ˆ๋‹ค๋Š” ๋œป

DI : ์˜์กด์„ฑ ์ฃผ์ž… (Bean ์ฃผ์ž…)

-> Spring์—์„œ ๊ตฌํ˜„์„ ์š”๊ตฌํ•˜๋Š” ๋ถ€๋ถ„๋“ค์€ interface๋กœ ์ •์˜, ์ดํ›„ ์‚ฌ์šฉ์ž๊ฐ€ ์ •์˜ํ•œ ๊ตฌํ˜„์ฒด Bean์„ ์‹ค์ œ ์„œ๋น„์Šค์—์„œ ์‚ฌ์šฉ

 

๐Ÿซง Spring๊ณผ Spring Boot์˜ ์ฐจ์ด

Spring : ์‹คํ–‰์„ ์œ„ํ•ด Tomcat ๊ฐ™์€ ํ”„๋กœ๊ทธ๋žจ์ด ํ•„์š”ํ•˜๋‹ค.
Spring Boot : Tomcat ๊ฐ™์€ ์„œ๋ฒ„ ํ”„๋กœ๊ทธ๋žจ์ด ๋‚ด์žฅ๋˜์–ด, Jar์˜ ํ˜•ํƒœ๋กœ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค.