본문 바로가기

개발(합니다)/Java&Spring

[spring boot 설정하기-10] dev-tools 설정 및 테스트 소스

반응형

개발을 하다보면 수정 된 소스가 자동으로 반영되도록 해서 편리하게 테스트 되기 바랄 때 사용합니다.

관련 정보는 아래 사이트에서 확인할 수 있습니다.

docs.spring.io/spring-boot/docs/1.5.16.RELEASE/reference/html/using-boot-devtools.html

www.baeldung.com/spring-boot-devtools

Spring boot devtools에서 제공하는 기능

1. Property Defaults

thymeleaf는 캐싱 기능을 사용하는데 캐싱되어 있으면 thymeleaf에 개발된 내용이 반영이 되지 않아 강력 새로 고침을 해야 하는 경우가 생깁니다. 이를 위해 cache를 false로 적용해 캐싱하지 않도록 적용할 수 있습니다.

캐싱을 사용하면 속도를 높일 수 있어서 운영과 개발 시 설정 값을 다르게 두어야 합니다.

2. Automatic Restart

서버단 소스에 수정 사항이 생기면 서버을 재시작해서 테스트해야 하는 경우가 생기는데 classpath에 존재하는 파일이 변경되면 자동으로 재시작해주는 기능입니다.

3. Live Reload

React나 Flutter와 같은 Hot reload처럼 js가 수정되면 자동으로 브라우저가 새로고침을 해주는 기능입니다.

chrome.google.com/webstore/detail/livereload/jnihajbhpnppcggbcgedagnkighmdlei?hl=ko

위에 크롬 확장 프로그램을 받고 application에 설정을 해주면 됩니다.

3-1. 의존성 추가 및 소스

의존성 추가

apply plugin: 'idea'
runtimeOnly('org.springframework.boot:spring-boot-devtools')

configurations {
    developmentOnly
    runtimeClasspath {
        extendsFrom developmentOnly
    }
}

 

테스트 소스 - DevtoolsContoller

package com.otrodevym.spring.base.common.devtools;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;


@Controller
public class DevtoolsContoller {


    @GetMapping("/devtools/reload")
    public String devtoolsReload(Model model) {
        model.addAttribute("devtools", "변경 후 ㅁㅁㅁㅁㅁ");

        model.addAttribute("test", "test");
        model.addAttribute("a", "a");
        return "devtools/devtools-test";
    }

}

 

테스트 소스 - devtools-test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>devtools test</title>
</head>
<body>
  devtools test init
  <p th:text="${devtools}"></p>
  <p th:text="${test}"></p>
  <p th:text="${test}"></p>
  <p th:text="${test}"></p>
</body>
</html>

3-2. Intellij 설정

Ctrl + Shift + A -> Registry..

Build, Excution, Deployment -> Compiler -> Build project automatically 체크

 

만약 잘 안된다면 inteelij에서 제공하는 기능을 설정

 

결과

4. Global Settings

spring에 대한 공통 설정을 할 수 있습니다.

$HOME/.config/spring-boot 디렉토리에 공통 설정을 담아 놓으면 됩니다.

5. Remote Applications

로컬 개발을 넘어 원격지원을 포함하고 있습니다.

 

기본 환경 설정

org.springframework.boot.devtools.RemoteSpringApplication 를 실행하고 원격 접속하고자 하는 주소를 입력합니다.

 

설정이 잘 안된 경우

 

반응형