본문 바로가기
Framekwork/SPRING

[SPRING] 스프링 부트 - 프로젝트 환경설정

by 아이엠제니 2023. 8. 28.

 

 


 

간단한 웹 애플리케이션 개발

  • 스프링 프로젝트 생성
  • 스프링 부트로 웹 서버 실행
  • 회원 도메인 개발
  • 웹 MVC 개발
  • DB 연동 - JDBC, JPA, 스프링 데이터 JPA
  • 테스트 케이스 작성

 


프로젝트 생성

사전 준비물
  • Java 11 설치
  • IDE: IntelliJ 또는 Celipse 설치

 

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

https://start.spring.io/

 

 

프로젝트
  • 프로젝트 선택
    • 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

 

Could not resolve all files for configuration ':classpath'.

해당 error 발생 시, jdk는 11로 한 상태에서 spring-boot 버전을 2.7.14 (2.x)로 함. (3.x)로 할 시 에러남

 

 

maven? gradle?

필요한 라이브러리를 당겨오고, 빌드하는 라이프사이클까지 관리해주는 툴

요즘은 gradle을 많이 사용하는 추세임

 

 

구조

  • src
    • main
      • java
      • resources
        • 실제 자바 코드 파일을 제외한 xml이나 properties와 같은 설정 파일들이 있음. html도 여기에 들어감
    • test (요즘 개발에서는 테스트 코드가 중요하다.)
    • build.gradle

 

 

HelloSpringApplication.java run

이 화면 뜨면 성공

 

 

💡 번외

gradle로 되어 있는 것을 IntelliJ IDEA로 변경한다.

gradle로 되어 있으면, gradle을 통해서 run이 되어서 느릴 때가 있다고 한다.

 

 

 


라이브러리 살펴보기

스프링 부트 라이브러리
  •  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 환경설정

 

Welcom Page 만들기

💾 resources/static/index.html

<!DOCTYPE html>
<html>
<head>
    <title>Hello</title>
    <meta charset="UTF-8" http-equiv="Content-Type" content="text/html" />
</head>
<body>
Hello
<a href="/hello">hello</a>
</body>
</html>

 

 

템플릿 엔진

 

  • controller
    • 웹 애플리케이션에서 진입점

 

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


💾 templates/hello.html

<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" http-equiv="Content-Type" content="text/html" />
    <title>Document</title>
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}">안녕하세요. 손님</p>
</body>
</html>

 

동작 환경

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

 

💡 `spring-boot-devtools` 라이브러리를 추가하면, `html` 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경이 가능함 (인텔리제이 컴파일 방법: 메뉴 build -> Recompile)

 

 

 


빌드하고 실행하기

콘솔로 이동

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

 

윈도우 cmd
  • 스프링 프로젝트가 있는 경로로 이동 후, `gradlew` 파일이 있는 것을 확인 후 아래 명령어 입력
gradlew build

  • `build` 폴더가 생성이 되었고, 그 안에 `libs` 폴더로 이동을 해보면 `jar` 파일이 생성된 것을 확인할 수 있음

 

java -jar hello-spring-0.0.1-SNAPSHOT.jar

  • 해당 명령어 입력하면 스프링이 실행되는 것을 확인할 수 있음

 

gradlew clean
  • `build` 폴더 없어짐

 

 

 

인프런 김영한 - <스프링 입문 - 코드로 배우는 스프링 부트, 웹 MVC, DB 접근 기술> 실습
300x250