Back-end/Spring-boot

Spring-boot 프로젝트 생성 및 구조

김병찬 2025. 1. 31. 13:44

Project 생성

  • Spring-boot 스타터 사이트로 이동해서 Spring project 생성
  • https://start.spring.io (Spring-boot 기반으로 Spring 관련 Project 를 만들어주는 Site)

 

Project

  • Maven
  • Gradle (요즘은 Gradle 을 많이 사용함)
  • 필요한 Library 를 가져오고, 빌드하는 lifecycle 을 관리해주는 tool

Spring-Boot

  • SNAPSHOT : 아직 만들고 있는 Version
  • M1 : 아직 정식 Release 된 Version X

Project Metadata

  • Group : 기업명 or 도메인명
  • Artifact : Build 되어 나올 때의 결과물
  • Name
  • Description
  • Package Name

Dependencies

  • 어떤 library 를 가져와서 사용할 것인가?
  • Web project를 만든다고 한다면 Spring Web, Thymeleaf (HTML을 만들어주는 Template engine -> 다른 것을 사용해도 무관함)

그 후 Generate 생성해서 생성함.

build.gradle 을 IDE 에서 선택하여 Open

 

Project 초기 구조

src

  • main : java, resources
  • test : test code 와 관련된 내용이 들어가 있음. (요즘은 test 가 제일 중요!)

build.gradle

  • version 설정, library 가져온다는 것만 지금은 설명
  • dependencies, .gitignore 등 다양한 것들 존재
  • main method 를 실행하면 "SpringApplication.run(HelloSpringApplication.class, args);" code 를 실행하면서 Spring-boot application 을 띄움
  • Spring-boot 에는 Tomcat 이라는 Web server 를 내장하고 있어서 실행하면 같이 올라옴.
참고
Java로 직접 실행되는 게 아닌 gradle 을 통해서 실행될 때가 존재하는데, 이렇게 되면 실행이 느려짐.
Intellij -> Preferences -> gradle -> Build and run using 을 Intellij 로 변경하면 됨.

 

Spring-boot Library

1️⃣ Spring-boot-starter-web

  • spring-boot-starter-tomcat : Tomcat (Web-server)
  • spring-webmvc : Spring web MVC

2️⃣ Spring-boot-starter-thymeleaf

  • Thymeleaf template engine

3️⃣ Spring-boot-starter (공통)

  • Spring-boot + Spring core + Logging
  • spring-boot -> spring core
  • spring-boot-starter-logging -> logback.slf4j

Test Library

1️⃣ Spring-boot-starter-test

  • junit : Test framework
  • mockito : 목 library
  • assertj : Test code 를 좀 더 편하게 작성할 수 있도록 도와주는 library
  • spring-test : spring 통합 테스트 지원

View 환경설정

1️⃣ Welcome page

  • "resources/static/index.html"
참고
Spring 이 제공하는 게 많다.
필요한 것들을 찾는 능력이 중요함.
"https://spring.io" -> projects -> spring-boot -> Learn -> 맞는 version의 Reference Doc 확인 -> 찾기

 

Spring-boot 가 제공하는 Welcome page 기능

  • "static/index.html" 을 올려두면 Welcome page 기능 제공
  • 정적 기능 (파일을 그대로)
<!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>

 

2️⃣ thymeleaf template engine

  • 원하는 대로 모양을 바꿀 수 있는 것이 template engine

동작하고 프로그래밍 되는 화면

  • Controller : Web application 에서 첫 번째 진입점
  • hello.hello_spring/controller/HelloController
  • controller 는 annotation (@Controller 적어줘야 함)
// templates/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>
  • th : thymeleaf
  • xmlns:th="http://www.thymeleaf.org : thymeleaf template engine 이 선언되어 있는데, thymeleaf 문법을 쓸 수 있도록 함.

thymeleaf template 동작 확인

 

  • Controller 에서 return 값으로 문자를 반환하면 viewResolver 가 화면을 찾아서 처리함.
  • Spring-boot template engine 기본 viewName Mapping
  • resources:templates/ + {viewName} + .html
참고
Spring-boot-devtools library 를 추가하면, html file을 compile 만 해주면 서버 재시작 없이 view 파일 변경 가능
Intellij Compile 방법 : 메뉴 build -> Recompile

 

Build & 실행

Teminal / Console 이동

  1. ./gradle build
  2. cd build/libs
  3. java -jar hello-spring-0.0.1-SNAP