Spring, Apache, Java

[Spring Boot] 서버 재시작을 하지 않고 static 파일 변경 적용하기

cwchoiit 2023. 10. 30. 15:42
728x90
반응형
SMALL

Spring boot로 개발을 하고 서버사이드 렌더링을 위해 Thymeleaf를 사용하다보면 template 파일 수정이 빈번하게 일어난다.

그 때마다 변경사항을 반영하기 위해 서버를 재시작하기는 꽤 귀찮은 일이다. 이 때 개발환경에서는 spring-boot-devtools라는 라이브러리를 사용해서 좀 더 간편하게 변경사항을 반영시킬 수 있다.

 

build.gradle

implementation 'org.springframework.boot:spring-boot-devtools'

build.gradle 파일에서 spring-boot-devtools 라이브러리를 하나 추가해준다.

 

서버를 재시작 하면 서버 실행 로그의 쓰레드의 이름이 restartedMain으로 보여진다. 이러면 spring-boot-devtools가 잘 적용된 것이다. thymeleaf 엔진을 사용하는 템플릿 파일이 다음과 같다고 해보자. 

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

이 템플릿을 뿌려주는 컨트롤러가 지정한 URL로 브라우저를 띄워 입력해 들어가보면 다음처럼 나온다.

 

내가 근데 다음과 같이 텍스트를 수정했을 때, 이 수정한 내용을 서버에 바로 반영하고 싶다.

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

그럴 때 이제 spring-boot-devtools로 서버를 실행한 후 Build > Recompile을 클릭하면 된다.

이렇게 하고 다시 들어가보면 변경 사항이 바로 적용되어 있다. 서버를 재시작하지 않아도 된다!

728x90
반응형
SMALL

 

728x90
반응형
LIST