4.如何讓SPRINGBOOT自動重啟


必須重啟

目前的Springboot,當發生了任何修改之后,必須關閉后再啟動Application類才能夠生效,顯得略微麻煩。 Springboot提供了熱部署的方式,當發現任何類發生了改變,馬上通過JVM類加載的方式,加載最新的類到虛擬機中。 這樣就不需要重新啟動也能看到修改后的效果了
 

pom.xml

 
做法很簡單,在pom.xml中新增加一個依賴和一個插件就行了。
依賴:
 
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> <!-- 這個需要為 true 熱部署才有效 -->
</dependency>
 

插件:
 
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
 
 
 
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
71
72
<? 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 >
             
         </ 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 >
               
         </ dependency >
               < dependency >
                      < groupId >javax.servlet</ groupId >
                      < artifactId >jstl</ artifactId >
               </ dependency >
         <!-- tomcat的支持.-->
         < dependency >
                < groupId >org.apache.tomcat.embed</ groupId >
                < artifactId >tomcat-embed-jasper</ artifactId >
                
         </ 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 >

重啟測試

 
重新啟動Application,然后隨便修改一下HelloController, 就會如圖所示觀察到控制台的自動重啟現象
重啟測試


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM