ํ๋ก์ ํธ ์์ฑ
Spring์ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ๊ธฐ๋ณธ์ ์ผ๋ก Java๋ฅผ ์ฌ์ฉํ๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์, Java๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก ์ค์นํ ์ ์์ด์ผ ํ๋ค.
์ธํ๋ฐ์ Spring์ ๋ฌธ๊ฐ์๋ ๊ธฐ๋ณธ์ ์ผ๋ก Java 11 ๋ฒ์ ์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ 11 ๋ฒ์ ์ ์ค์นํ๋ ๊ฒ ์ข๋ค.
IDE(ํตํฉ ๊ฐ๋ฐ ํ๊ฒฝ)๋ IntelliJ ๋๋ Eclipse๋ฅผ ์ค์นํ๋ฉด ๋๋ค.
์ด๋ ๊ฒ ์ค๋น๊ฐ ๋์๋ค๋ฉด ํ๋ก์ ํธ๋ฅผ ์์ฑํ๋ฉด ๋๋ค.
์๋ ๊ฐ์ ๊ฒฝ์ฐ์๋ Springํ๋ก์ ํธ๋ฅผ ๋ฐ๋ฐ๋ฅ๋ถํฐ ๋ง๋ค์๋ ๋ฐ๋ฉด์,
์์ฆ์๋ ์คํ๋ง ๋ถํธ๋ฅผ ์ฌ์ฉํด ์ฝ๊ฒ ์์ฑ์ด ๊ฐ๋ฅํ๋ค.
* ์คํ๋ง ๋ถํธ ์คํํฐ ์ฌ์ดํธ๋ก ์ด๋ํด์ ์คํ๋ง ํ๋ก์ ํธ ์์ฑ *
์ ์ฌ์ง์ ์คํ๋ง ๋ถํธ ์คํํฐ ์ฌ์ดํธ์ ํ์ด์ง ์ฌ์ง์ด๋ค.
์ ์ผ ์ฒ์์ Project์์ Maven๊ณผ Gradle์ ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฐ์ง๊ณ ์ค๊ณ
๋น๋ํ๋ ๋ผ์ดํ์ฌ์ดํด๊น์ง ๊ด๋ฆฌ๋ฅผ ํด์ฃผ๋ ํด์ด๋ค.
๊ณผ๊ฑฐ์๋ Maven์ ๋ง์ด ์ฌ์ฉํ์ง๋ง ํ์ฌ๋ Greadle์ ์ฌ์ฉํ๋ ์ถ์ธ์ด๋ค.
Language ๊ฐ์ ๊ฒฝ์ฐ์๋ Java๋ฅผ ํ์ธํ๋ฉด ๋๋ค.
Spring Boot๋ ๋ฒ์ ์์ SNAPSHOT์ ์์ง ๋ง๋ค๊ณ ์๋ ๋ฒ์ ์ด๊ณ ,
M5๋ ์์ง ์ ์ ๋ฆด๋ฆฌ์ฆ ๋ ๋ฒ์ ์ด ์๋๋ค.
๊ทธ๋ฌ๋ฏ๋ก ๋ฒ์ ๋ง ํ์๋ ๋ฒ์ ์ค ๊ฐ์ฅ ๋์ ๋ฒ์ ์ ์ฌ์ฉํ๋ฉด ๋๋ค.
Project Metadata์ Group์ ๋ณดํต์๋ ๊ธฐ์ ๋ช ์ ์ ๋๋ค. ์ง๊ธ์ Spring๊ณต๋ถ๋ฅผ ์ํ ๊ฒ์ด๋ hello๋ฅผ ๊ธฐ์ฌํ๋ค.
Artifact๋ ํ๋ก์ ํธ๋ช ๊ฐ์ ๊ฒ์ด๋ค. hello-spring์ ๊ธฐ์ฌํ๋ค.
Dependencies๋ ์ง๊ธ ์คํ๋ง ๋ถํธ๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ํ๋ก์ ํธ๋ฅผ ์์ํ ๋ ์ด๋ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๊ฒ์ธ์ง ๊ฒฐ์ ํ๋ ๋ถ๋ถ์ด๋ค.
web ํ๋ก์ ํธ๋ฅผ ๋ง๋ค ๊ฒ์ด๋ spring web๊ณผ html์ ๋ง๋ค์ด์ฃผ๋ ํ ํ๋ฆฟ ์์ง์ด ํ์ํ๋ thymeleaf๋ฅผ ์ ํํ๋ค.
๊ทธ๋ฆฌ๊ณ GENERATE๋ฅผ ๋๋ฅด๋ฉด ๋ค์ด๋ก๋๋ฅผ ํ๊ฒ ๋๋ค. ๋ค์ด๋ก๋๊ฐ ๋๋๋ฉด ์์ถ์ ํด์ ํ๊ณ
IntelliJ์์ hello-spring> build.gradle์ open ํ๋ฉด ๋๋ค.
๊ทธ๋ฌ๋ฉด ํ๋ก์ ํธ๊ฐ ํ๋ฉด์ ๋จ๋ ๊ฑธ ํ์ธํ ์ ์๋ค.
์ฒ์ ํ๋ก์ ํธ๋ฅผ ์ด๊ฒ ๋๋ฉด ์ธ๋ถ์์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ค์ด๋ก๋ํ๊ธฐ ๋๋ฌธ์ ๋ค์ด๋ก๋๊ฐ ์งํ์ด ๋๋ค.
srcํด๋๋ฅผ ํ์ธํ๋ฉด main๊ณผ test๊ฐ ์๋ค. ์์ฆ ๊ฐ๋ฐ ํธ๋ ๋์์๋ test code๊ฐ ๋งค์ฐ ์ค์ํ๋ค.
๊ทธ๋ฆฌ๊ณ , build.gradle์ ํ์ธํ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
plugins {
id 'org.springframework.boot' version '2.7.4' → ์คํ๋ง ๋ถํธ ๋ฒ์
id 'io.spring.dependency-management' version '1.0.14.RELEASE'
id 'java'
}
group = 'hello' → Group ๋ช
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11' → Java ๋ฒ์
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
build.gradle์ dependencies๋ฅผ ํ์ธํ๋ฉด ์คํ๋ง ๋ถํธ์์ ์ ํํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ด ์๋ ๊ฑธ ํ์ธํ ์ ์๋ค.
์ด๋, ์ด๋์ ๋ค์ด๋ก๋ํ ์ง ์ ํ๋ ๊ฒ์ด repositories์ mavenCentral()์ด๋ค.
ํ์ํ๋ค๋ฉด ํน์ ์ฌ์ดํธ์ url์ ๊ธฐ์ฌํ ์๋ ์๋ค.
์ด์ main์ HelloSpringApplication์ main ๋ฉ์๋๋ฅผ ์คํํด๋ณด๋ฉด Spring์ด๋ผ๋ ๊ธ์๊ฐ ํฌ๊ฒ ์ฝ์์ ์ถ๋ ฅ์ด ๋๋ฉฐ,
Tomcat์ด ์คํ๋๋ ๊ฒ์ ํ์ธํ ์ ์๋ค. ๊ทธ๋ฆฌ๊ณ localhost:8080์ ๋๋ฉ์ธ์ ์ ๋ ฅ์ ํ๋ฉด Error page๊ฐ ๋ฌ๋ค.
Errorํ์ด์ง๊ฐ ๋ฌ๋ค๋ฉด ์ ์์ ์ผ๋ก ์คํ์ด ๋๊ณ ์๋ค๋ ๊ฒ์ด๋ค.
๋ํ, ์คํ์ ํ ๋ ๋น ๋ฅด๊ฒ ์คํ์ด ๋๊ฒ ํ๊ธฐ ์ํด์๋ File> Settings์์ gradle์ ๊ฒ์ํ์ฌ Build and run์์
Gradle๋ก ๋์ด์๋ ๊ฒ์ IntelliJ๋ก ๋ณ๊ฒฝํด์ฃผ๋ฉด ๋๋ค.
์ด๋ ๊ฒ ์ค์ ์ ํด์ฃผ๋ฉด Run ํ์ ๋ Gradle์ ํตํ์ง ์๊ณ , ๋ฐ๋ก IntelliJ๋ก ์ถ๋ ฅ์ ํ๊ธฐ ๋๋ฌธ์ ๋ ๋น ๋ฅด๊ฒ ์คํ์ด ๋๋ค.
๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ดํด๋ณด๊ธฐ
External Libraries๋ฅผ ํ์ธํด๋ณด๋ฉด ๋๋ thymeleaf์ spring web ๋ผ์ด๋ธ๋ฌ๋ฆฌ 2๊ฐ๋ฅผ ๊ฐ์ ธ์์ง๋ง,
์๋ง์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ค์น๋์๋๊ฒ์ ํ์ธํ ์ ์๋ค.
์ด๊ฒ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ ๊ธฐ๋ณธ์ ์ผ๋ก ์์กด๊ด๊ณ์ด๊ธฐ ๋๋ฌธ์ด๋ค.
spring web ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋
spring web ๋ผ์ด๋ธ๋ฌ๋ฆฌ์์ ์ฌ์ฉํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ค์ ๋ชจ๋ ๋ค ๊ฐ์ ธ์ค๊ฒ ๋๋ค.
์ด๋ฌํ ์์กด๊ด๊ณ๋ IntelliJ์ ๋งจ ์ค๋ฅธ์ชฝ Gradle์์ ํ์ธ์ด ๊ฐ๋ฅํ๋ค.
์ด๋ ๊ฒ ์์ ์๋ Tomcat์๋ฒ๋ฅผ ์ง์ ์ค์นํ๊ณ ํ ํ์ ์์ด, ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํ๋๋ง ์ค์นํด๋ ๋ชจ๋ ๊ฒ ์ค์น๊ฐ ๊ฐ๋ฅํ๋ค.
Gradle์ ์์กด๊ด๊ณ๊ฐ ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํจ๊ป ๋ค์ด๋ก๋ ํ๋ค.
์คํ๋ง ๋ถํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
→ spring-boot-starter-web
→ spring-boot-starter-tomcat: ํฐ์บฃ (์น์๋ฒ)
→ spring-webmvc: ์คํ๋ง ์น MVC
→ spring-boot-starter-thymeleaf: ํ์๋ฆฌํ ํ ํ๋ฆฟ ์์ง(View)
→ spring-boot-starter(๊ณตํต): ์คํ๋ง ๋ถํธ + ์คํ๋ง ์ฝ์ด + ๋ก๊น
→ spring-boot
→ spring-core
→ spring-boot-starter-logging
→ logback, slf4j
ํ ์คํธ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
→ spring-boot-starter-test
→ junit: ํ ์คํธ ํ๋ ์์ํฌ
→ mockito: ๋ชฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
→ assertj: ํ ์คํธ ์ฝ๋๋ฅผ ์ข ๋ ํธํ๊ฒ ์์ฑํ๊ฒ ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
→ spring-test:์คํ๋ง ํตํฉ ํ ์คํธ ์ง์
View ํ๊ฒฝ์ค์
localhost:8080์ ์ ๋ ฅํ์ ๋, Error ํ์ด์ง๊ฐ ๋จ๋ ๊ฑธ ํ์ธํ ์ ์์๋ค.
์ด์ ๋ Errorํ๋ฉด์ด ์๋, Welcomeํ์ด์ง์ ๋ฌธ์๊ฐ ์ถ๋ ฅ๋๊ฒ ํด๋ณด์.
welcomeํ์ด์ง๋ ๋๋ฉ์ธ์ ์ ๋ ฅํ์ ๋ ์ ์ผ ์ฒซ ํ๋ฉด์ด๋ค.
src> main> resources> static์ index.html ํ์ผ์ ์์ฑํ๋ค.
<!DOCTYPE HTML> <html>
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head>
<body> Hello
<a href="/hello">hello</a> </body>
</html>
๊ทธ๋ฆฌ๊ณ ์ด๋ ๊ฒ ๊ฐ๋จํ html ์ฝ๋๋ฅผ ์ฌ์ฉํด hello๋ฌธ์์ hello๋งํฌ๋ฅผ ๋ง๋ค์ด์ฃผ๊ณ ์คํํ๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
์ด๋ ๊ฒ Errorํ๋ฉด์ด ์๋, Hello๊ฐ ํ๋ฉด์ ์ถ๋ ฅ๋๋ค.
์ด๋ ๊ฒ ์คํ๋ง ๋ถํธ์์๋ static/index.html์ ์ฌ๋ ค๋๋ฉด Welcome page๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
์คํ๋ง์ ๋งค์ฐ๋งค์ฐ ๊ฑฐ๋ํ๊ณ ๋ฐฉ๋ํ ๊ธฐ๋ฅ์ ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ ๋จธ๋ฆฌ์ ๋ชจ๋ ๋ด์ ์ ์๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์, ํ์ํ ๊ฒ์ ์ฐพ์ ์ ์๋ ๋ฅ๋ ฅ์ด ๋งค์ฐ ์ค์ํ๋ค.
spring.io์์ spring์ ๋ค์ํ ๊ธฐ๋ฅ์ ๋ํด ์ ์ ์๋ค.
์ index.html์ ์ ์ ํ์ด์ง์ด๋ค. ๊ทธ๋ฅ ๊ทธ๋๋ก ์น์๋ฒ์๊ฒ html ํ์ผ์ ๋ณด๋ด๋ ๊ฒ์ด๋ค.
ํ์ง๋ง, ํ ํ๋ฆฟ ์์ง์ ์ฌ์ฉํ๋ฉด ๋์ ์ธ ํจ๊ณผ๋ฅผ ์ค ์ ์๋ค.
์ฌ๊ธฐ์๋ thymeleaf๋ผ๋ ํ ํ๋ฆฟ ์์ง์ ์ฌ์ฉํ ๊ฒ์ด๋ค.
thymeleaf ํ ํ๋ฆฟ ์์ง
→ thymeleaf ๊ณต์ ์ฌ์ดํธ: https://www.thymeleaf.org/
→ ์คํ๋ง ๊ณต์ ํํ ๋ฆฌ์ผ: https://spring.io/guides/gs/serving-web-content/
→ ์คํ๋ง ๋ถํธ ๋ฉ๋ด์ผ: https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot-features.html#boot-features-spring-mvc-template-engines
์ด์ thymeleaf๋ฅผ ์ฌ์ฉํด ๊ฐ๋จํ๊ฒ ๋์ํ๊ณ ํ๋ก๊ทธ๋๋ฐ์ด ๊ฐ๋ฅํ ์นํ์ด์ง๋ฅผ ๋ง๋ค์ด ๋ณด์.
์น ์ ํ๋ฆฌ์ผ์ด์ ์์ ์ฒซ๋ฒ์งธ ์ง์ ์ ์ ์ปจํธ๋กค๋ฌ(controller)์ด๋ค.
hello.hellospring๋ฐ์ controller๋ผ๋ ํจํค์ง๋ฅผ ์์ฑํ๋ค.
controller์์ HelloController Class๋ฅผ ์์ฑํด์ค๋ค.
HelloController์์ Class๋ด์ฉ์ ๋ค์๊ณผ ๊ฐ๋ค.
package hello.hellospring.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class HelloController {
@GetMapping("hello")
public String hello(Model model) {
model.addAttribute("data", "hello!!");
return "hello";
}
}
์ฌ๊ธฐ์ @GetMapping์ ("hello")๋ ์น ์ ํ๋ฆฌ์ผ์ด์ ์์ /hello๋ก ๋ค์ด์ค๋ฉด, ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ๊ฒ ๋๋ค.
hello ๋ฉ์๋์ model์ MVC์ Model์ ๊ทธ Model์ด๋ค.
๊ทธ๋ฆฌ๊ณ , templates์์ hello.html ํ์ผ์ ์์ฑํ๋ค. hello.html์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Hello</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'์๋
ํ์ธ์. ' + ${data}" >์๋
ํ์ธ์. ์๋</p>
</body>
</html>
hello.html์ ${data}๋ HelloController์ data์ value๊ฐ์ธ hello!๋ก ์นํ์ด ๋๋ค.
๊ทธ๋ ๊ฒ ์คํ์ ํด์ ํ์ธ์ ํด๋ณด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
์ด๋ ๊ฒ /helloํ์ด์ง์์ ์๋ ํ์ธ์. hello! ๊ฐ ๋จ๋ ๊ฑธ ํ์ธํ ์ ์๋ค.
์์ ๊ฐ์ ๊ณผ์ ์ ์๋์ ์ด๋ฏธ์ง์ ๊ฐ์ ๊ณผ์ ์ด๋ค.
์ปจํธ๋กค๋ฌ์์ ๋ฆฌํด ๊ฐ์ผ๋ก ๋ฌธ์๋ฅผ ๋ฐํํ๋ฉด ๋ทฐ ๋ฆฌ์กธ๋ฒ(viewResolver)๊ฐ ํ๋ฉด์ ์ฐพ์์ ์ฒ๋ฆฌํ๋ค.
→ ์คํ๋ง ๋ถํธ ํ ํ๋ฆฟ ์์ง ๊ธฐ๋ณธ viewName ๋งคํ
→ resources:templates/ + {ViewName} + .html
์ฐธ๊ณ : spring-boot-devtools ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ถ๊ฐํ๋ฉด, html ํ์ผ์ ์ปดํ์ผ๋ง ํด์ฃผ๋ฉด
์๋ฒ ์ฌ์์ ์์ด View ํ์ผ ๋ณ๊ฒฝ์ด ๊ฐ๋ฅํ๋ค.
์ธํ ๋ฆฌJ ์ปดํ์ผ ๋ฐฉ๋ฒ: ๋ฉ๋ด build → Recompile
๋น๋ํ๊ณ ์คํํ๊ธฐ
์ด์ IntelliJ์์ ์คํํ๋ ๊ฒ์ด ์๋, ํฐ๋ฏธ๋์์ ์คํ์ ํด๋ณด์.
๋จผ์ IntelliJ์์ ์คํ ์ค์ด๋ ๊ฒ์ ์ข ๋ฃํด์ผ ํ๋ค. ( ๊ฐ์ ์๋ฒ๋ฅผ ๋์์ ์คํํ ์ ์๋ค. )
๋จผ์ ํฐ๋ฏธ๋์์ hello-spring ๋๋ ํฐ๋ฆฌ๋ก ์ด๋์ ํด์ค๋ค.
๊ทธ๋ฆฌ๊ณ gradlew build๋ฅผ ์ ๋ ฅํ๋ฉด ํ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ค์ด๋ฐ์ผ๋ฉด์ ๋น๋๋ฅผ ํ๋ค.
๋น๋๊ฐ ์๋ฃ๋ ํ, hello-spring/build/libs ๋๋ ํฐ๋ฆฌ๋ก ์ด๋ํ๋ค.
๊ทธ๋ฆฌ๊ณ java -jar hello-spring-0.0.1-SNAPSHOT.jar์ ์คํํ๋ฉด, Spring์ด ์ถ๋ ฅ๋๋ฉด์
ํฐ๋ฏธ๋์์ ์คํ์ด ๊ฐ๋ฅํด์ง๋ค.
'๐ฟ Framework > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JPA์์ ๋ณ๊ฒฝ ๊ฐ์ง์ ๋ณํฉ(Merge) ์ดํดํ๊ธฐ (0) | 2025.03.26 |
---|---|
JPA๋? ๊ฐ๋ , ์์์ฑ ์ปจํ ์คํธ, JPQL๊น์ง ํ๋ฒ์ ์ ๋ฆฌ (1) | 2025.03.06 |
Thymeleaf ํ ํ๋ฆฟ ์์ง (2) | 2024.10.20 |
[Spring]๊ฐ์ฒด ์งํฅ ์ค๊ณ์ ์คํ๋ง (0) | 2022.10.22 |