springboot热部署未生效怎么办

来源:互联网 发布:新浪博客做淘宝客网站 编辑:程序博客网 时间:2024/05/22 00:26

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>
原创粉丝点击