1. 必須重啟
目前的Springboot,當發(fā)生了任何修改之后,必須關閉后再啟動Application類才能夠生效,顯得略微麻煩。 Springboot提供了熱部署的方式,當發(fā)現(xiàn)任何類發(fā)生了改變,馬上通過JVM類加載的方式,加載最新的類到虛擬機中。 這樣就不需要重新啟動也能看到修改后的效果了
2. pom.xml
做法很簡單,在pom.xml中新增加一個依賴就行了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional> true </optional> <!-- 這個需要為 true 熱部署才有效 --> </dependency> <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion> 4.0 . 0 </modelVersion> <groupId>com.how2java</groupId> <artifactId>springboot</artifactId> <version> 0.0 . 1 -SNAPSHOT</version> <name>springboot</name> <description>springboot</description> <packaging>war</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 1.5 . 9 .RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 3.8 . 1 </version> <scope>test</scope> </dependency> <!-- servlet依賴. --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!-- tomcat的支持.--> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional> true </optional> <!-- 這個需要為 true 熱部署才有效 --> </dependency> </dependencies> <properties> <java.version> 1.8 </java.version> </properties> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
3.重啟測試
重新啟動Application,然后隨便修改一下HelloController, 就會如圖所示觀察到控制臺的自動重啟現(xiàn)象
補充:
下面看下SpringBoot自動重啟、熱啟動
SpringBoot自動重啟的兩種方法:
1)在項目的pom中直接添加plugin,如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <!-- 熱部署 --> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version> 1.2 . 6 .RELEASE</version> </dependency> </dependencies> </plugin> </plugins> </build> |
當對classPath(包含javadiamante以及其他配置文件等)里的文件操作完成保存的時候,項目會自動重啟,免去了手動重啟項目的麻煩;
2)使用springBoot為我們提供的工具類,在pom中添加依賴;
1
2
3
4
5
6
7
|
<dependencys> <!--SpringBoot開發(fā)工具 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencys> |
DevTools是SpringBoot提供的開發(fā)工具,在激活了開發(fā)者工具以后,classpath 里對文件進行任何操作都會觸發(fā)應用程序重新啟動。SpringBoot開發(fā)者工具在重新啟動時會排除 /META-INF/resources 、/resources 、/static 、/public 和/templates ; 可以設置Spring.devtools.restart.exclude 屬性來覆蓋默認的重啟排除目錄 ;
如果想要關閉自動重啟 則可以這樣設Spring.devtools.restart.enable= false;設置觸發(fā)文件 必須修改這個觸發(fā)文件才能觸發(fā)重啟 spring.devtools.restart.trigger-file 屬性 ;當應用程序以完整打包好的jar或war文件運行時,開發(fā)者工具會被禁用。 激活開發(fā)者工具后,Spring boot 會啟動一個內嵌的LiveReload服務器,在資源文件發(fā)生變化時會觸發(fā)刷新瀏覽器。你要做的就是在瀏覽器中安裝LiveReload; 如果想要排除內嵌的瀏覽器 Spring.devtools.livereload.enableled=false ;
總結
以上所述是小編給大家介紹的springboot自動重啟的簡單方法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網(wǎng)站的支持!
原文鏈接:https://blog.csdn.net/wo_shi_LTB/article/details/79794690