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

z00m__in·2022년 5월 17일
0
post-thumbnail

프로젝트 환경 설정

프로젝트 생성

기본 프로그램 설치 확인

  • Java 11 설치
  • IDE: IntelliJ 또는 Eclipse 설치
    추세) 실무에서는 전자 많이 사용

프로젝트 생성 @ 스프링 부트 스터디 사이트

https://start.spring.io/
추세) 스프링 프로젝트를 대부분 스프링 부트를 기반으로 생성

  • Project: Maven / Gradle (필요한 라이브러리 사용 및 build 하는 cycle 관리하는 tool)
  • Language: Java / Kotlin / Groovy
  • Spring Boot: SNAPSHOT(정식 release X), M1, 아닌 것 중 최신
  • Project Metadata
    - Group: 기업명 및 도메인명
    • Artifact: build되어 나온 결과물로, 프로젝트명과 유사
    • Name
    • Description
    • Package name
  • Dependencies : 스프링부트 기반으로, 어떤 library를 추가할 지
    Web 개발 중이므로 'Spring Web', 'Thymeleaf'(jtml 만들어주는 템플렛 엔진)

IntelliJ 실행

  • 생성한 프로젝트의 다운로드
  • zip 파일 압축 해제
  • IntelliJ로 파일의 build.gradle 열기
  • 설정의
  • src > main > java > hello.hellospring > HelloSpringApplication > main method 실행

라이브러리 살펴보기

External Library에 여러 라이브러리 있음 > 여기서 사용중인 라이브러리확인 가능

스프링 부트 라이브러리

  • spring-boot:starter-web
  • spring-boot:starter-thymeleaf
  • spring-boot:starter

테스트 라이브러리

  • spring-boot:starter-test
    • junit: 테스트 프레임워크
    • mockito: 목 라이브러리
    • assertj: 테스트 코드를 좀 더 편하게 작성하게 도와주는 라이브러리
    • spring-test: 스프링 통합 테스트 지원

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>
<a href="/hello">hello</a>
</body>
</html>

https://docs.spring.io/spring-boot/docs/2.6.7/reference/htmlsingle/#web

Thymeleaf 템플릿 엔진

HelloController

hello.hellospring/controller/HelloController.java

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
    public String hello(Model model){
        model.addAttribute("data", "hello");
        return "hello";
    }
}

hello.html

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>

빌드하고 실행하기

cmd 이용해서 실행 가능 (IntelliJ 말고)

  • ./gradlew build
  • cd build/libs
  • java -jar hello-spring-0.0.1-SNAPSHOT.jar

본 포스트는 김영하의 <스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술> 강좌를 바탕으로 작성한 포스트입니다.

profile
우당탕탕 기록지

0개의 댓글