1. 程式人生 > >SpringBoot基礎-如何讓springboot自動重啟

SpringBoot基礎-如何讓springboot自動重啟

  1.  必須重啟
    目前的Springboot,當發生了任何修改之後,必須關閉後再啟動Application類才能夠生效,顯得略微麻煩。 Springboot提供了熱部署的方式,當發現任何類發生了改變,馬上通過JVM類載入的方式,載入最新的類到虛擬機器中。 這樣就不需要重新啟動也能看到修改後的效果了
  2.  pom.xml
    做法很簡單,在pom.xml中新增加一個依賴就行了
     
    <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, 就會如圖所示觀察到控制檯的自動重啟現象