springboot的熱布署

原理:

  1. 在class字節(jié)碼發(fā)生變動(dòng)時(shí),可以讓classloader重新加載,而達(dá)到不重啟應(yīng)用的效果。
  2. 以下介紹的兩種工具都使用了相同的手段:更換了類加載器?。?!
    devtools.restart.classloader.RestartClassLoader
    而不是
    sun.misc.Launcher$AppClassLoader
  3. 日志中的變化:
    如果日志出現(xiàn)了 [ restartedMain] ,即表示更換成功!
2019-08-22 13:05:59.761  INFO 3504 --- [  restartedMain] cn.johnyu.demo02.Demo02Application       : 

工具:

一、 springloaded

1. 配置方法 pom.xml:
    <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.8.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
2. 啟動(dòng)方法:
mvn spring-boot:run
3. 缺點(diǎn):

只能使用maven方式啟動(dòng),否則無法達(dá)到目的。

二、spring-boot-devtools

1. 配置:
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
2. 啟動(dòng):

無限制

3. 需要注意:

(1)在application.properties中加入[可選操作]:

#禁止thymeleaf緩存(建議:開發(fā)環(huán)境設(shè)置為false,生成環(huán)境設(shè)置為true)
spring.thymeleaf.cache=false

#添加那個(gè)目錄的文件需要restart
spring.devtools.restart.additional-paths=src/main/java
#排除那個(gè)目錄的文件不需要restart
spring.devtools.restart.exclude=static/**,public/**

(2)在idea進(jìn)行配置【必選】:

打開自動(dòng)自動(dòng)構(gòu)建功能

Preferences -> Build, Execution, Deployment -> Compiler,勾選Build project automatically

運(yùn)行期自動(dòng)編譯(不重啟動(dòng)應(yīng)用):

進(jìn)入Registry...中(Mac使用快捷鍵shift+option+command+/,window上的快捷鍵是Shift+Ctrl+Alt+/
勾選:compiler.automake.allow.when.app.running

4. 禁用此功能:

硬編碼:System.setProperty("spring.devtools.restart.enabled", "false");
application.properties: spring.devtools.restart.enabled 注意原因不明,不起作用

較全的pom.xml中的片段:

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <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.8.RELEASE</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容