Skip to content

Commit 9ec508f

Browse files
committed
热部署
1 parent e71b1f6 commit 9ec508f

File tree

5 files changed

+53
-0
lines changed

5 files changed

+53
-0
lines changed

docs/springboot/initializr.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,59 @@ public class HelloController {
170170

171171
OK,现在可以访问到了。也就表明我们的第一个 Spring Boot 项目开发完成了。
172172

173+
### 热部署
174+
175+
作为开发者,我们希望每次修改代码后,代码能够自动编译,服务能够自动重新加载,这样就省去了重新运行代码的烦恼。
176+
177+
那 spring-boot-devtools 就是这样的一个神器,当我们把它添加到项目当中后,无论是代码修改,还是配置文件修改,服务都能够秒级重载(俗称热部署),这在我们开发的时候,非常有用。
178+
179+
怎么才能开启热部署呢?
180+
181+
第一步,在 pom.xml 文件中添加依赖。
182+
183+
```
184+
<dependency>
185+
<groupId>org.springframework.boot</groupId>
186+
<artifactId>spring-boot-devtools</artifactId>
187+
</dependency>
188+
```
189+
190+
第二步,没有第二步了,哈哈。
191+
192+
直接修改代码来体验一下吧。我们修改 HelloController 内容为:
193+
194+
```java
195+
@Controller
196+
public class HelloController {
197+
198+
@GetMapping("/hello")
199+
@ResponseBody
200+
public String hello() {
201+
return "hello, springboot,沉默王二是傻X";
202+
}
203+
}
204+
```
205+
206+
由于 Intellij IDEA 是自动保存的,所以默认情况下,Intellij IDEA 的实时编译是关闭的(建议不要打开,否则已修改就编译,挺废内存的),我们需要手动点一下 build 按钮。
207+
208+
![](http://cdn.tobebetterjavaer.com/tobebetterjavaer/images/springboot/initializr-55098eb9-1809-460d-9d2a-730da24e73c0.png)
209+
210+
很快,控制台就开始滚动日志了。
211+
212+
![](http://cdn.tobebetterjavaer.com/tobebetterjavaer/images/springboot/initializr-489f64c7-8d1e-4aea-a5c9-bad582991040.png)
213+
214+
我们发现,这次服务重载的时间很短,短到只有 0.372 秒,这就很方便了有没有,不用再去点运行/调试按钮重新跑服务了,重新跑服务是很费时间的,大家可以操作一下试试。
215+
216+
217+
![](http://cdn.tobebetterjavaer.com/tobebetterjavaer/images/springboot/initializr-377b9d53-0bcd-475d-9774-4135a9019694.png)
218+
219+
再次访问 `http://localhost:8080/hello`,就可以看到返回的内容发生变化了。
220+
221+
222+
![](http://cdn.tobebetterjavaer.com/tobebetterjavaer/images/springboot/initializr-a5924f5c-c41f-49d7-b076-b93d24de37c8.png)
223+
224+
也就是说,热部署生效了。搞定。
225+
173226
### 源码路径
174227

175228
>- 编程喵:[https://github.com/itwanger/coding-more](https://github.com/itwanger/coding-more)
852 KB
Loading
460 KB
Loading
374 KB
Loading
174 KB
Loading

0 commit comments

Comments
 (0)