• [Spring] ๊น€์˜ํ•œ ์Šคํ”„๋ง ์ž…๋ฌธ <์„น์…˜1.ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ์„ค์ •> ์ •๋ฆฌ

    2023. 10. 1.

    by. @leeeun

    ๊ฐ•์˜ ์†Œ๊ฐœ

    ์‹ค๋ฌด์—์„œ ๋™์ž‘ํ•˜๋Š” ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜

    ์›น MVC ์ค‘์š”ํ•˜๋‹ค

    Database ์— ์ €์žฅ→ db์— ์ ‘๊ทผํ•˜๋Š” ๊ธฐ์ˆ 

    ์Šคํ”„๋ง ๋ถ€ํŠธ

     

    ์„น์…˜1

     

    ํ”„๋กœ์ ํŠธ ํ™˜๊ฒฝ์„ค์ •

     

    ์‚ฌ์ „์„ค์น˜

    • ๊ฐ•์˜๋‚ด์šฉ Java11์„ค์น˜ → ๋ณธ์ธ์€ ์Šคํ”„๋ง 3.0 ์ด์ƒ ์‚ฌ์šฉ ์œ„ํ•ด 17 ์„ค์น˜
    • IDE: IntelliJ ๋˜๋Š” Eclipse ์„ค์น˜

    ๋„ค์ด๋ฒ„, ์นด์นด์˜ค, ์ฟ ํŒก, ๋ฐฐ๋ฏผ ๋“ฑ ์„œ๋ฒ„ ๊ฐœ๋ฐœ์ž ์ดํด๋ฆฝ์Šค ๋Œ€๋ถ€๋ถ„ ์‚ฌ์šฉxx

    ์š”์ฆ˜์€ ๋‹ค ์Šคํ”„๋ง ๋ถ€ํŠธ๋ฅผ ์ด์šฉํ•ด์„œ ์Šคํ”„๋ง ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑ

    https://start.spring.io/

     

     

    ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

    • ํ”„๋กœ์ ํŠธ ์„ ํƒ
      • Project: Gradle - Groovy Project
      • Maven vs Gradle๊ณผ๊ฑฐ์—๋Š” Maven → ์š”์ฆ˜์—๋Š” ์‹ค๋ฌด์—์„œ ๊ฑฐ์˜ Gradle๋กœ ๋„˜์–ด์˜ค๋Š” ์ถ”์„ธ
      • ํ•„์š”ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋•ก๊ฒจ์„œ ์˜ค๊ณ , ๋นŒ๋“œํ•˜๋Š” ๋ผ์ดํ”„ ์‚ฌ์ดํ‹€๊นŒ์ง€ ๊ด€๋ฆฌํ•ด์ฃผ๋Š” ํˆด
      • Spring Boot: 2.3.x (Snapshot ์•„์ง ๋งŒ๋“ค๊ณ  ์žˆ๋Š” ๋ฒ„์ „ M๋ถ™์€ ๊ฒƒ๋“ค๋„ ์ •์‹ ๋ฆด๋ฆฌ์ฆˆ๋œ ๋ฒ„์ „x) → 3.1.4
      • Language: Java
      • Packaging: Jar
      • Java: 11 → 17์œผ๋กœ
    • Project Metadata # ํ”„๋กœ์ ํŠธ ์ •๋ณด ๊ธฐ์ž…
      • groupId: hello #๋ณดํ†ต ๊ธฐ์—…๋ช… ๊ฐ™์€ ๊ฒƒ์„ ์ ์Œ
      • artifactId: hello-spring # ํ”„๋กœ์ ํŠธ๋ช…๊ฐ™์€ ๊ฒƒ
        • Dependencies: Spring Web, Thymeleaf(html์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํ…œํ”Œ๋ฆฟ ์—”์ง„) #์–ด๋–ค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋•ก๊ฒจ์„œ ์‚ฌ์šฉํ• ์ง€

    → Generate

    → ๋‹ค์šด ๋ฐ›์€ ์••์ถ• ํŒŒ์ผ์„ ์›ํ•˜๋Š” ํด๋”์— ๋„ฃ์€ ํ›„ ์••์ถ• ํ•ด์ œ

    → ์ธํ…”๋ฆฌ์ œ์ด์—์„œ open or import

    → build.gradle์„ ์„ ํƒ ํ›„ open as a project

    ํ”„๋กœ์ ํŠธ ๊ตฌ์„ฑ

     

    • .idea ์ธํ…”๋ฆฌ์ œ์ด๊ฐ€ ์‚ฌ์šฉํ•˜๋Š” ์„ค์ •ํŒŒ์ผ
    • gradle
    • src
      • main #์ž๋ฐ” ๊ด€๋ จ ์‹ค์ œ ํŒจํ‚ค์ง€, ์†Œ์Šค ํŒŒ์ผ๋“ค
        • resources #์ž๋ฐ” ์ฝ”๋“œ ํŒŒ์ผ์„ ์ œ์™ธํ•œ xml, properties, ์„ค์ •ํŒŒ์ผ ๋“ฑ ์ž๋ฐ” ํŒŒ์ผ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€๋“ค ์ „๋ถ€
      • test #ํ…Œ์ŠคํŠธ ์ฝ”๋“œ์™€ ๊ด€๋ จ๋œ ์†Œ์Šค๋“ค → ์š”์ฆ˜ ๊ฐœ๋ฐœ ํŠธ๋ Œ๋“œ์—์„œ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๊ฐ€ ๋งค์šฐ ์ค‘์š”
      • build.gradle #spring boot๊ฐ€ ์ œ๊ณต ๋ฒ„์ „ ์„ค์ •ํ•˜๊ณ  ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ๋•ก๊ฒจ์˜จ๊ฑฐ
        • mavenCentral() - ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹ค์šด๋ฐ›๋Š” MavenCentral ์‚ฌ์ดํŠธ
        • dependencies ์šฐ๋ฆฌ๊ฐ€ ๋•ก๊ฒจ์˜จ thymeleaf, springweb ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ๊ธฐ๋ณธ์ ์œผ๋กœ ๋“ค์–ด์žˆ๋Š” test๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
      • .gitignore #์†Œ์Šค์ฝ”๋“œ ๊ด€๋ฆฌ ๊นƒ์—๋Š” ์†Œ์Šค ์ฝ”๋“œ ํŒŒ์ผ๋งŒ ์˜ฌ๋ผ๊ฐ€๊ณ  ๋นŒ๋“œ๋œ ๊ฒฐ๊ณผ๋ฌผ ์˜ฌ๋ผ๊ฐ€๋ฉด ์•ˆ ๋˜๋Š”๋ฐ ์ด๋Ÿฐ ๊ฒƒ์„ ๊ด€๋ฆฌ
      • gradlew
      • gradlew.bat
      • settings.gradle #์šฐ์„  ๋„˜์–ด๊ฐ~

     

    ํ”„๋กœ์ ํŠธ ์‹คํ–‰ํ•˜๊ธฐ

     

    src→main→java→HelloSpringApplication

    compact middle packages ํ•ด๋„ ๋˜๊ณ  ์•ˆํ•ด๋„ ๋˜๊ณ ~

    ์ž๋ฐ”๋Š” ๋ฉ”์ธ ๋ฉ”์†Œ๋“œ๋กœ๋ถ€ํ„ฐ ๋ชจ๋“  ํ”„๋กœ์ ํŠธ๊ฐ€ ์‹œ์ž‘ → ๋ฉ”์ธ ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰

    ํ„ฐ๋ฏธ๋„ ๋ฉ”์„ธ์ง€ Tomcat started on port(s): 8080 (http) with context path → localhost 8080 ํฌํŠธ๋กœ ๋ฌด์—‡์ด ์ƒ์„ฑ๋จ

    ์›น ๋ธŒ๋ผ์šฐ์ €๋กœ localhost:8080 → ์—๋Ÿฌ ํŽ˜์ด์ง€ ๋œจ๋ฉด ์„ฑ๊ณต!

    • Springboot๋Š” ํ†ฐ์บฃ์ด๋ผ๋Š” ์›น์„œ๋ฒ„๋ฅผ ๋‚ด์žฅ
    • @SpringBootApplication ์–ด๋…ธํ…Œ์ด์…˜ → ์Šคํ”„๋ง๋ถ€ํŠธ๊ฐ€ ํ†ฐ์บฃ ์›น์„œ๋ฒ„๋ฅผ ์ž์ฒด์ ์œผ๋กœ ๋„์šฐ๋ฉด์„œ ์Šคํ”„๋ง๋ถ€ํŠธ๊ฐ€ ๊ฐ™์ด ์˜ฌ๋ผ์˜ด

     

    IntelliJ Gradle ๋Œ€์‹ ์— ์ž๋ฐ” ์ง์ ‘ ์‹คํ–‰

     

    • ์ตœ๊ทผ IntelliJ ๋ฒ„์ „์€ Gradle์„ ํ†ตํ•ด์„œ ์‹คํ–‰ ํ•˜๋Š” ๊ฒƒ์ด ๊ธฐ๋ณธ ์„ค์ •์ด๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์‹คํ–‰์†๋„๊ฐ€ ๋Š๋ฆฌ๋‹ค. ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜๋ฉด ์ž๋ฐ”๋กœ ๋ฐ”๋กœ ์‹คํ–‰ํ•ด์„œ ์‹คํ–‰์†๋„๊ฐ€ ๋” ๋น ๋ฅด๋‹ค.
    • IntelliJ IDEA→ Settings→ Gradle ๊ฒ€์ƒ‰ → Build and run using & Run test using Gradle์ด ์•„๋‹Œ ์ธํ…”๋ฆฌ์ œ์ด๋กœ ๋ณ€๊ฒฝ (์‹คํ–‰์†๋„ ๋น ๋ฅด๊ฒŒ)

     

    ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ดํŽด๋ณด๊ธฐ

     

    • Gradle, Maven๊ฐ™์€ tool๋“ค์ด ์˜์กด๊ด€๊ณ„๋ฅผ ๋‹ค ๊ด€๋ฆฌ
    • SpringBootStarterWeb์„ ์ฃผ์ž…ํ•˜๋ฉด ์ด๋ฅผ ๊ฐ€์ ธ์˜ค๊ธฐ ์œ„ํ•ด ์ด์™€ ์˜์กด๊ด€๊ณ„์— ์žˆ๋Š” ํ•„์š”ํ•œ tomcat, web ๋“ฑ์„ gradle์ด ๋‹ค ๊ฐ€์ ธ์˜ด

    ์™ผ์ชฝ ๋ฐ‘์— ์‚ด์ง ๋ˆ„๋ฅด๋ฉด gradle ๊ฑฐ๋ถ์ด๊ฐ€ ๋œธ

     

    External Libraries

     

    • ์ด์ „์˜ ๊ฐœ๋ฐœ ๋ฐฉ์‹) ์›น์„œ๋ฒ„๋ฅผ ์ง์ ‘ ์„ค์น˜ → ๊ฑฐ๊ธฐ์— ์ž๋ฐ”์ฝ”๋“œ๋ฅผ ๋ฐ€์–ด๋„ฃ๋Š” ๋ฐฉ์‹์œผ๋กœ ์›น์„œ๋ฒ„์™€ ๊ฐœ๋ฐœ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ๊ตฌ๋ถ„๋˜์–ด ์žˆ๋Š” ๋ฐฉ์‹ (ํž˜๋“  ์ž‘์—…)
    • ์š”์ฆ˜์˜ ๊ฐœ๋ฐœ ๋ฐฉ์‹) ์Šค์Šค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ ์›น ์„œ๋ฒ„ ์ž์ฒด๋ฅผ ๋‚ด์žฅ(embeded) ์ž๋ฐ” ๋ฉ”์ธ ๋ฉ”์†Œ๋“œ๋งŒ ์‹คํ–‰ํ•ด๋„ ์›น์„œ๋ฒ„๋ฅผ ๋„์›Œ์คŒ

     

    ์Šคํ”„๋ง ๋ถ€ํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

     

    • 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
        • ์‹ค๋ฌด์—์„œ๋Š” system.out์œผ๋กœ ์ถœ๋ ฅx log๋ฅผ ํ†ตํ•ด ์ถœ๋ ฅ(์—๋Ÿฌ ๊ด€๋ฆฌ, ๋กœ๊ทธ ํŒŒ์ผ ๊ด€๋ฆฌ๋ฅผ ์œ„ํ•จ)
        • logback, slf4j (๋‘ ์กฐํ•ฉ์„ ๋งŽ์ด ์‚ฌ์šฉ)
        • logging(๋กœ๊น…)์ด๋ž€?๋กœ๊น… ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ข…๋ฅ˜๋กœ log4j์™€ logback์ด ์กด์žฌ
        • ๋กœ๊ทธ(log)๋ฅผ ์ƒ์„ฑํ•˜๋„๋ก ์‹œ์Šคํ…œ์„ ์ž‘์„ฑํ•˜๋Š” ํ™œ๋™
        • logback vs slf4jlogback ์‹ค์ œ ๋กœ๊ทธ๋ฅผ ์–ด๋–ค ๊ตฌํ˜„์ฒด๋กœ ์ถœ๋ ฅํ• ์ง€
        • slf4j ๋กœ๊น… ํ”„๋ ˆ์ž„ ์›Œํฌ์— ๋Œ€ํ•œ ์ธํ„ฐํŽ˜์ด์Šค ์—ญํ• ์„ ํ•˜๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

     

    ํ…Œ์ŠคํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ

     

    • spring-boot-starter-test
      • junit: ํ…Œ์ŠคํŠธ ํ”„๋ ˆ์ž„์›Œํฌ (ํ…Œ์ŠคํŠธํ• ๋•Œ ์ž๋ฐ”๋Š” ๋Œ€๋ถ€๋ถ„ ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉ 5๋ฅผ ๋งŽ์ด ์“ฐ๋Š” ์ถ”์„ธ)
      • mockito: ๋ชฉ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
      • assertj: ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์ข€ ๋” ํŽธํ•˜๊ฒŒ ์ž‘์„ฑํ•˜๊ฒŒ ๋„์™€์ฃผ๋Š” ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
      • spring-test: ์Šคํ”„๋ง ํ†ตํ•ฉ ํ…Œ์ŠคํŠธ ์ง€์›

    assertj, mockito ํ…Œ์ŠคํŠธ ํŽธ๋ฆฌํ•˜๊ฒŒ ๋„์™€์ฃผ๋Š” ๋„๊ตฌ

     

    view ํ™˜๊ฒฝ์„ค์ •

     

    (1) ์ •์  ํŽ˜์ด์ง€ → ์›น ์„œ๋ฒ„๊ฐ€ ํŒŒ์ผ์„ ๊ทธ๋Œ€๋กœ ์›น ์„œ๋ฒ„์— ๋„์šฐ๋Š” ๊ฒƒ

    • spring boot →src/resources/static ํด๋”์— ์žˆ๋Š” index.html์„ ๋จผ์ € ์ฐพ๋Š”๋‹ค ๋ชป ์ฐพ์œผ๋ฉด ์ธ๋ฑ์Šค ํ…œํ”Œ๋ฆฟ์„ ์ฐพ๋Š” ์‹์œผ๋กœ ๋™์ž‘
    • ์ผ์ข…์˜ welcome page๊ฐ€ ๋จ
    • https://docs.spring.io/spring-boot/docs/2.3.1.RELEASE/reference/html/spring-boot- features.html#boot-features-spring-mvc-welcome-page
    • ๋ฉ”๋‰ด์–ผ์—์„œ ์ง์ ‘ ๊ฒ€์ƒ‰ํ•˜๋ฉด์„œ ๊ณต๋ถ€ํ•ด๋ณด๊ธฐ

     

    (2) thymeleaf ํ…œํ”Œ๋ฆฟ ์—”์ง„ ์‚ฌ์šฉ→ ๋ณ€ํ™”๋ฅผ ์ค„ ์ˆ˜ ์žˆ์Œ

    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"; //hello.html์œผ๋กœ ๊ฐ€์„œ ํ™”๋ฉด์„ ์‹คํ–‰์‹œ์ผœ๋ผ
        }
    
    }
    
    • ์›น ๋ธŒ๋ผ์šฐ์ €์—์„œ hello → ๋‚ด์žฅ๋˜์–ด์žˆ๋Š” ํ†ฐ์บฃ ์„œ๋ฒ„๊ฐ€ ์Šคํ”„๋งํ•œํ…Œ ๋˜์ง → ์Šคํ”„๋ง์˜ helloController์—์„œ addAttribute๊ฐ€ ์‹คํ–‰ → return “hello” resources/templates/hello.html์„ ์‹คํ–‰ํ•œ๋‹ค๋Š” ๋œป
    • ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ๋ฆฌํ„ด ๊ฐ’์œผ๋กœ ๋ฌธ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋ทฐ ๋ฆฌ์กธ๋ฒ„( viewResolver )๊ฐ€ ํ™”๋ฉด์„ ์ฐพ์•„์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.
      • ์Šคํ”„๋ง ๋ถ€ํŠธ ํ…œํ”Œ๋ฆฟ์—”์ง„ ๊ธฐ๋ณธ viewName ๋งคํ•‘ #viewName=hello
      • resources:templates/ +{ViewName}+ .html

     

    ๋นŒ๋“œํ•˜๊ณ  ์‹คํ–‰ํ•˜๊ธฐ

     

    ์ฝ˜์†”๋กœ ์ด๋™

    1. ./gradlew build
    2. cd build/libs
    3. java -jar hello-spring-0.0.1-SNAPSHOT.jar ⇒ ์„œ๋ฒ„ ๋ฐฐํฌ์‹œ์—๋Š” ์ด .jar ํŒŒ์ผ๋งŒ ๋ณต์‚ฌํ•ด์„œ ์„œ๋ฒ„์— ๋„ฃ๊ณ  Java -jar.exe๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ๋จ (์„œ๋ฒ„์—์„œ ์Šคํ”„๋ง์ด ๋™์ž‘)
    4. ์‹คํ–‰ํ™•์ธ

     

    http get ๋ฉ”์†Œ๋“œ post method

    ./gradlew clean build → build ํด๋” ์ž์ฒด๊ฐ€ ์™„์ „ํžˆ ์ง€์›Œ์ง€๊ณ  ๋‹ค์‹œ ๋นŒ๋“œ

     

    ๋Œ“๊ธ€