스프링 SPRING/[SPRING] 김영한 스프링 입문

[SPRING 입문] 1. 프로젝트 환경 설정

CHANGEL 2023. 1. 19. 08:24

1. 프로젝트 생성

사전 준비

  • Java 11 설치
  • IDE: IntelliJ 또는 Eclipse 설치 (저는 IntelliJ IDEA Ultimate 버전으로 진행하였습니다.)

주의! 가급적 JDK 11버전을 설치를 권장한다. 다른 버전을 설치하면 정상 작동하지 않을 가능성이 높음.

 

  • 프로젝트 선택
    • Project: Gradle Project
    • Spring Boot: 2.3.x
    • Language: Java
    • Packaging: Jar
    • Java: 11
  • Project Metadata
    • groupId: hello
    • artifactId: hello-spring
  • Dependencies: Spring Web, Thymeleaf

Gradle 전체 설정

build.gradle

plugins {
    id 'org.springframework.boot' version '2.3.1.RELEASE'
    id 'io.spring.dependency-management' version '1.0.9.RELEASE'
    id 'java'
}
  group = 'hello'
  version = '0.0.1-SNAPSHOT'
  sourceCompatibility = '11'

  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') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}
  test {
    useJUnitPlatform()
}

 

  • 동작 확인
    • 기본 메인 클래스 실행
    • 스프링 부트 메인 실행 후 에러페이지로 간단하게 동작 확인 (http://localhost:8080)

IntelliJ Gradle 대신 자바 직접 실행

최근 IntelltJ 버전은 Gradle을 통해서 실행 하는 것이 기본 설정이다. 이렇게 하면 실행속도가 느리다. 다음과 같이 변경하면 자바로 바로 실행해서 실행속도가 더 빠르다.

  • Preferences -> Build, Execution, Deployment -> Build Tools -> Gradle
    • Build and run using: Gradle -> IntelliJIDEA
    • Run tests using: Gradle -> IntelliJ IDEA

IntelliJ JDK 설치 확인

IntelliJ에서 자바 실행이 잘 안되면 다음 부분을 확인하자.(일반적으로 자동으로 설정이 되어 있지만, 가끔 문제가 되는 경우에 참고)

  • 프로젝트 JDK 설정 -> 11로 지정
  • Gradle JDK 설정 -> 11로 지정

 

2. 라이브러리 살펴보기

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: 스프링 통합 테스트 지원

 

3. View 환경설정

Welcome Page 만들기

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>

 

thymeleaf 템플릿 엔진

@Controller
  public class HelloController {
      @GetMapping("hello")
      public String hello(Model model) {
          model.addAttribute("data", "hello!!");
          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>

thymeleaf 템플릿엔진 동작 확인

동작 환경 그림

  • 컨트롤러에서 리턴 값으로 문자를 반환하면 뷰 리졸버(viewResolver)가 화면을 찾아서 처리한다.
    • 스프링 부트 템플릿엔진 기본 viewName 매핑
    • resources: templates/ + {ViewName} + .html

 

4. 빌드하고 실행하기

콘솔로 이동

  1. ./gradlew build
  2. cd build/libs
  3. java -jar hello-spring-0.0.1-SNAPSHOT.jar
  4. 실행 확인

윈도우 사용자를 위한 팁

  1. 콘솔로 이동 -> 명령 프롬프트(cmd)로 이동
  2. ./gradlew -> gradlew.bat를 실행하면 된다.
  3. 명령 프롬프트에서 gradlew.bat를 실행하려면 gradlew하고 엔터를 치면 된다.
  4. gradlew build
  5. 폴더 목록 확인 ls -> dir