스프링부트 Devtools

Devtools는 스프링부트가 제공하는 옵셔널한 도구이다.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-Devtools</artifactId>
</dependency>

Devtools를 이용하기 위해서는 spring-boot-Devtools 의존성을 추가해야한다. 해당 의존성을 추가하면 기본적으로 적용되는 properties가 존재한다. 기본설정 코드를 조금만 살펴보자.

static {
		Map<String, Object> properties = new HashMap<>();
		properties.put("spring.thymeleaf.cache", "false");
		properties.put("spring.freemarker.cache", "false");
		properties.put("spring.groovy.template.cache", "false");
		properties.put("spring.mustache.cache", "false");
		properties.put("server.servlet.session.persistent", "true");
		properties.put("spring.h2.console.enabled", "true");
		properties.put("spring.resources.cache.period", "0");
		properties.put("spring.resources.chain.cache", "false"); // 1
		properties.put("spring.template.provider.cache", "false");
		properties.put("spring.mvc.log-resolved-exception", "true");
		properties.put("server.error.include-stacktrace", "ALWAYS");
		properties.put("server.servlet.jsp.init-parameters.development", "true");
		properties.put("spring.reactor.stacktrace-mode.enabled", "true");
		PROPERTIES = Collections.unmodifiableMap(properties);
}

캐시는 기본값으로 false설정된다. 캐시가 적용되어 있으면 변경사항이 바로 적용되지 않기 때문에 개발환경에 맞도록 캐시기능들을 false로 설정한다.

Automatic Restart

캐시를 끄거나 하는 기본기능보다 중요한건 자동 재시작 기능이다. 클래스패스에 있는 파일이 변경될 때마다 자동으로 재시작된다. Devtools가 제공하는 자동 재시작은 우리가 서버를 restart하는 것(cold restart)보다 빠르다.

The restart technology provided by Spring Boot works by using two classloaders

보다 더 빠른 이유를 살펴보자. 스프링부트는 두개의 클래스로더를 사용한다. base classloader는 우리가 변경하지 않는 의존성(third-part jars)을 로드한다. restart classloader는 우리가 개발하는 애플리케이션을 로드한다. Devtools에 의해 애플리케이션이 다시시작 될 때 restart classloader는 제거되고 재생성된다. 반면, base classloader에서 로드된 외부 의존성은 제거되지 않고 다시 사용되기 때문에 일반적으로 cold restart보다 훨씬 빠르다.

spring.Devtools.restart.exclude를 이용하여 원하는 리소스를 모니터링에서 제외시킬 수 있다.

Restart or Reload

서버를 재시작하는 기능은 restart이며 브라우저까지 같이 갱신하는 기능은 livereload 기능이다. livereload는 브라우저에 플러그인을 설치해야 한다. 하지만, livereload는 실패확률이 높아서 더 불편할 수도 있다.

리모트 애플리케이션

리모트 애플리케이션은 원격지에서 동작하고 있는 애플리케이션을 종료하거나 재시작할 수 있는 기능이다. 이는 운영용이 아닌 개발용이다.

해당 포스팅은 스프링 부트 개념과 활용 강의 내용을 토대로 작성하였습니다.