springboot熱部署未生效怎麼辦

沸騰的可樂發表於2017-11-10

springboot熱部署未生效怎麼辦

熱部署:當發現程式修改時自動啟動應用程式。

spring boot為開發者提供了一個名為spring-boot-devtools的模組來使Spring Boot應用支援熱部署,提高開發者的開發效率,無需手動重啟Spring Boot應用。

原理

使用了兩個ClassLoader,一個Classloader載入那些不會改變的類(第三方Jar包),另一個ClassLoader載入會更改的類,稱為restart ClassLoader,這樣在有程式碼更改的時候,原來的restart ClassLoader 被丟棄,重新建立一個restart ClassLoader,由於需要載入的類相比較少,所以實現了較快的重啟時間。

配置方式

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional><!-- optional配置為true -->
</dependency>
    ...

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
            <version>1.2.7.RELEASE</version>
        </dependency>
    </dependencies>
    <configuration><fork>true</fork></configuration>
</plugin>

如何生效

假如用mvn spring-boot:run 方式啟動應用,修改完java檔案就會立即生效

為什麼熱部署明明配置正確還未生效

假如用debug 方式啟動應用,需要手動重新編譯整個專案,或者被修改的檔案
比如idea下點選選單 build > build XXX 或者選中檔案 build >recompile “${your java file}”

手動編譯觸發熱部署

筆者環境

mac pro+idea2016
jdk1.8+maven3+spring4 + springboot1.5.7.RELEASE+springloaded1.2.7.RELEASE

PS 完整pom配置

<?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.XXX</groupId>
    <artifactId>XX</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <name>bi</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>


        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.4</version>
        </dependency>

        <!-- jdbc driver -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.36</version>
        </dependency>

        <!--print sql-->
        <dependency>
            <groupId>com.googlecode.log4jdbc</groupId>
            <artifactId>log4jdbc</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.12</version>
        </dependency>

        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.5-FINAL</version>
        </dependency>


        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional><!-- optional=true,依賴不會傳遞,該專案依賴devtools;之後依賴myboot專案的專案如果想要使用devtools,需要重新引入 -->
        </dependency>
    </dependencies>

    <build>
        <finalName>XX</finalName>
        <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.7.RELEASE</version>
                    </dependency>
                </dependencies>
                <configuration><fork>true</fork></configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <skip>true</skip>
                </configuration>
            </plugin>
        </plugins>
    </build>


</project>

相關文章