[Spring] 스프링 입문 - 1. 프로젝트 환경 설정

사전 준비


Spring 시작 전에 Java 17 이상 설치 및 IDE(IntelliJ or Eclipse) 설치해야 함!

 

[JAVA] JDK 버전 변경(IntelliJ 버전 변경)

 

[JAVA] JDK 버전 변경(IntelliJ 버전 변경)

JDK 설치 원하는 JDK 버전 선택해서 운영체제에 맞게 설치 https://www.oracle.com/kr/java/technologies/downloads/ Download the Latest Java LTS Free Subscribe to Java SE and get the most comprehensive Java support available, with 24/7 global

codingmoding.tistory.com

 

 

 

프로젝트 생성


스프링 부트 스타터 사이트로 이동해서 스프링 프로젝트 생성

 

https://start.spring.io

2024.01.05 기준

 

 

build.gradle

plugins {
	id 'java'
	id 'org.springframework.boot' version '3.2.1'
	id 'io.spring.dependency-management' version '1.1.4'
}

group = 'hello'
version = '0.0.1-SNAPSHOT'

java {
	sourceCompatibility = '17'
}

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()
}

 

 

 

크롬에서 실행

  • 기본 메인 클래스(HelloSpringApplication) 실행

  •  주소창에 http://localhost:8080 입력

 

 

 

라이브러리 살펴보기


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 환경 설정


resources/static/index.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>

: 정적 페이지이므로 static 폴더 밑에 위치

 

 

  • localhost:8080 으로 접속

 

 

thymeleaf 템플릿 엔진

: 컨트롤러에서 리턴 값으로 문자를 반환하면 viewResolver가 화면 찾아서 처리

 

  • 스프링 부트 템플릿엔진 기본 viewName 매핑
  • resources:templates/ +{ViewName}+ .html

 

 

java/hello.hellospring/controller/HelloController

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", "spring!!");
        return "hello";
    }
}

 

 

resources/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>

 

 

콘솔창에서 빌드하고 실행

1. hello-spring 폴더로 이동

C:\> cd hello-spring

 

 

2. gradlew.bat build 실행

C:\hello-spring> gradlew.bat build

 

 

3. 폴더 목록 확인

C:\hello-spring> dir

 

 

4. build\libs 폴더로 이동

C:\hello-spring> cd build\libs

 

 

5. java 실행

C:\hello-spring\build\libs> java -jar hello-spring-0.0.1-SNAPSHOT.jar

 

 

※ 에러 발생한 경우 gradlew clean 혹은 gradlew.bat clean 사용해보기

gradlew.bat clean

 

'Back-end > Spring' 카테고리의 다른 글

[Spring] 프로젝트 초기화 방법  (0) 2024.01.13