IT 강좌(IT Lectures)/SpringBoot

10강. Spring Boot DevTools

소울입니다 2024. 7. 23. 08:06
728x90
반응형

 

챕터 10: Spring Boot DevTools

10.1 DevTools 개요 및 설정

10.1.1 DevTools의 역할과 기능

Spring Boot DevTools는 개발 생산성을 높이기 위한 도구입니다. DevTools는 코드 변경 시 애플리케이션을 자동으로 재시작하고, 브라우저를 자동으로 새로고침하는 기능을 제공합니다. 이를 통해 개발자는 더 빠르고 효율적으로 작업할 수 있습니다.

역사적 배경:

  • 과거에는 코드 변경 후 애플리케이션을 수동으로 재시작해야 했기 때문에 개발 속도가 느렸습니다.
  • Spring Boot DevTools는 이러한 문제를 해결하기 위해 도입되었으며, 개발 환경에서의 편의성을 크게 향상시켰습니다.

10.1.2 설정 방법

Spring Boot DevTools를 사용하려면 먼저 spring-boot-devtools 의존성을 추가해야 합니다.

<!-- pom.xml -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>

DevTools는 개발 환경에서만 활성화되므로, production 환경에서는 자동으로 비활성화됩니다.

10.2 자동 재시작 및 라이브 릴로드

10.2.1 자동 재시작 설정

Spring Boot DevTools는 코드 변경을 감지하여 애플리케이션을 자동으로 재시작합니다. 이를 통해 개발자는 코드 변경 후 애플리케이션을 수동으로 재시작할 필요가 없습니다.


# application.properties
spring.devtools.restart.enabled=true
    

아래는 자동 재시작이 작동하는 예제입니다:


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@RestController
class HelloController {

    // HTTP GET 요청을 처리하여 "Hello, World!"를 반환하는 메서드
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }
}
    

위의 예제에서 HelloController 클래스의 내용을 변경하면, DevTools가 변경 사항을 감지하여 애플리케이션을 자동으로 재시작합니다.

10.2.2 라이브 릴로드 설정

라이브 릴로드 기능은 코드 변경 시 브라우저를 자동으로 새로고침하여 변경 사항을 즉시 반영합니다. 이를 위해서는 LiveReload 브라우저 확장 프로그램을 설치해야 합니다.


# application.properties
spring.devtools.livereload.enabled=true
    

아래는 라이브 릴로드가 작동하는 예제입니다:


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@Controller
class WebController {

    // HTTP GET 요청을 처리하여 "index" 뷰를 반환하는 메서드
    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("message", "Hello, World!");
        return "index";
    }
}
    

위의 예제에서 index.html 파일을 변경하면, DevTools가 변경 사항을 감지하여 브라우저를 자동으로 새로고침합니다.

10.3 개발 환경 최적화

10.3.1 개발 편의성 향상 방법

Spring Boot DevTools는 개발 환경에서 다양한 편의 기능을 제공합니다. 예를 들어, 캐시를 비활성화하여 코드 변경 사항을 즉시 반영할 수 있습니다.


# application.properties
spring.thymeleaf.cache=false
    

10.3.2 DevTools 사용 팁

Spring Boot DevTools를 효과적으로 사용하는 몇 가지 팁을 소개합니다:

    • 자동 재시작 제외 경로 설정: 특정 경로의 변경 사항을 감지하지 않도록 설정할 수 있습니다. 예를 들어, static 파일은 자동 재시작에서 제외할 수 있습니다.

spring.devtools.restart.exclude=static/**
        
    • 전체 재시작과 컨텍스트 캐시: DevTools는 기본적으로 빠른 재시작을 위해 컨텍스트를 캐시합니다. 전체 재시작이 필요할 경우, 컨텍스트 캐시를 비활성화할 수 있습니다.

spring.devtools.restart.additional-exclude=**/common/**
        
    • 자동 재시작 디버깅: DevTools의 자동 재시작 기능이 예상대로 동작하지 않을 경우, 디버그 로그를 활성화하여 문제를 확인할 수 있습니다.

logging.level.org.springframework.boot.devtools=DEBUG
        

10.4 추가 예제

예제 1: RESTful 서비스 자동 재시작

이 예제는 간단한 RESTful 서비스를 정의하고, 자동 재시작 기능을 테스트합니다.


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@RestController
class GreetingController {

    // HTTP GET 요청을 처리하여 "Hello, {name}!"을 반환하는 메서드
    @GetMapping("/greet/{name}")
    public String greet(@PathVariable String name) {
        return "Hello, " + name + "!";
    }
}
    

이 예제에서는 GreetingController 클래스의 내용을 변경하면, DevTools가 변경 사항을 감지하여 애플리케이션을 자동으로 재시작합니다.

예제 2: LiveReload와 함께 하는 Thymeleaf 템플릿 변경

이 예제는 Thymeleaf 템플릿 파일을 변경하고, LiveReload 기능을 통해 브라우저가 자동으로 새로고침 되는 과정을 보여줍니다.


package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@Controller
class WebController {

    // HTTP GET 요청을 처리하여 "home" 뷰를 반환하는 메서드
    @GetMapping("/home")
    public String home(Model model) {
        model.addAttribute("message", "Welcome to the home page!");
        return "home";
    }
}
    
<!-- src/main/resources/templates/home.html -->
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Home</title>
</head>
<body>
    <h1 th:text="${message}"></h1>
</body>
</html>

이 예제에서 home.html 파일을 변경하면, LiveReload가 이를 감지하여 브라우저를 자동으로 새로고침합니다.

예제 3: 개발 시 캐시 비활성화

이 예제는 캐시 비활성화를 통해 개발 시 변경 사항이 즉시 반영되도록 합니다.


# application.properties
spring.thymeleaf.cache=false
    

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

@RestController
class MessageController {

    // HTTP GET 요청을 처리하여 "Hello, Spring Boot DevTools!"를 반환하는 메서드
    @GetMapping("/message")
    public String message() {
        return "Hello, Spring Boot DevTools!";
    }
}
    

이 예제에서는 MessageController 클래스의 내용을 변경하면, DevTools가 변경 사항을 감지하여 애플리케이션을 자동으로 재시작하고, 브라우저에서 변경된 내용을 즉시 확인할 수 있습니다.

반응형

'IT 강좌(IT Lectures) > SpringBoot' 카테고리의 다른 글

12강. 스프링부트와 국제화(I18N)  (0) 2024.07.25
11강. 프론트엔드 통합  (1) 2024.07.23
9강. Spring Boot Actuator  (0) 2024.07.22
8강. 예외 처리  (0) 2024.07.19
7강. 데이터 접근  (0) 2024.07.18