[Spring Boot] 프로젝트 환경설졍
Updated:
프로젝트 생성
- Java 11 (나는 Java 8을 이미 설치)
 - IDE : IntelliJ
 
스프링 부트 스타터 사이트로 이동해서 스프링 프로젝트 생성 https://start.spring.io/


HelloSpringApplication.java
package com.example.hellospring;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloSpringApplication {
	public static void main(String[] args) {
		SpringApplication.run(HelloSpringApplication.class, args);
	}
}
실행시키면 localhost:8080에서 서버가 실행되는 것을 볼 수 있음
번외
IntelliJ를 사용하면 빌드가 java를 직접 실행하는 것이 아니라 gradle을 통해 실행될 때가 있음, gradle을 통해 실행되면 느릴때가 있음
Preferences > gradle 검색 > IntelliJ IDEA로 바꿔주기

라이브러리 살펴보기
Gradle은 의존관계가 있는 라이브러리를 함께 다운로드

tomcat 예전에는 웹 서버와 개발 라이브러리가 완전히 분리되어 있었음. (tomcat, 라이브러리 모두 설치해야 했음)
요즘은 소스 라이브러리에서 웹 서버를 내장하고 있음. 그래서 실행하면 웹 서버가 뜨고 접속 가능
스프링 부트 라이브러리
- spring-boot-starer-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: 테스트 프레임워크 (junit5)
 - mockito: 목 라이브러리
 - assertj: 테스트 코드를 조금 더 편하게 작성하도록 도와주는 라이브러리
 - spring-test: 스프링 통합 테스트 지원
 
 
View 환경설정
Welcome Page 만들기
- 스프링 부트가 제공하는 Welcome Page 기능
 
Welcome Page 만들기 : localhost:8080 들어가자마자 나오는 페이지 (ndex.html을 static 에서 찾음)
src/main/resources/static/index.html
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
Hello
<a href="/hello">hello</a>
</body>
</html>
서버 재실행

spring.io -> Spring Boot -> LEARN -> Reference Document에서 필요한 기능을 검색
Thymeleaf 템플릿 엔진
템플릿 엔진을 사용하면 정적 페이지가 아니라 모양을 바꿀 수 있음
- thymeleaf 공식 사이트 : https://www.thymeleaf.org/
 - 스프링 공식 튜토리얼 : https://spring.io/guides/gs/serving-web-content/
 - 
    
스프링 부트 : https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/
 - Controller = Web Application 에서 첫번째 진입점
 
src/main/java/com.example.hellospring -> Controller 패키지 생성
Controller -> HelloController.java 생성
@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>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello</title>
</head>
<body>
<p th:text="'안녕하세요. ' + ${data}" >안녕하세요. 손님</p>
</body>
</html>
서버 재실행하고, localhost:8080/hello 접속

Thymeleaf 템플릿 엔진 동작

- 컨트롤러에서 리턴 값으로 문자를 반환하면 뷰 리졸버(viewResolver)가 화면을 찾아서 처리한다.
    
- 스프링 부트 템플릿 엔진 기본 viewName 매핑
 - resources:templates/ + (ViewName) + .html
 
 
참고 spring-boot-devtools 라이브러리를 추가하면, html 파일을 컴파일만 해주면 서버 재시작 없이 View 파일 변경이 가능. 인텔리제이 컴파일 방법: 메뉴 build ➡️ Recompile
빌드하고 실행하기
- 콘솔로 이동
    
- ./gradlew build
 - cd build/libs
 - java -jar hello-spring-0.0.1-SNAPSHOT.jar
 - 실행 확인
 
 


결과

빌드가 잘 안 될 때 ./gradlew clean : build 파일을 없앨 수 있음. build 파일 지우고 다시 ./gradlew build
      
    
Leave a comment