美文网首页
2、Spring Boot 热部署

2、Spring Boot 热部署

作者: 杜艳_66c4 | 来源:发表于2020-10-13 16:01 被阅读0次

Spring Boot -02- Spring Boot 热部署

在目前的 Spring Boot 项目中,当发生了任何修改之后我们都需要重新启动才能够正确的得到效果,非常麻烦,Spring Boot 提供了热部署的方式,当发现任何类发生了改变,就会通过 JVM 类加载的方式,加载最新的类到虚拟机中,这样就不需要重新启动也能看到修改后的效果了。

(一)热部署前 IDEA 中的一些设置

(1)打开自动构建项目:



2)在编辑器中,同时按下:Ctrl + Shift + Alt + ?(/) 四个键,

3)点击第一个Registry,找到截图中的选项,打上勾选:


(二) pom.xml 文件中的一些设置

(1)在 pom.xml 的 dependencies 中添加内容:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional> <!-- 这个需要为 true 热部署才有效 -->
        </dependency>

(2)在 pom.xml 的 plugins 中添加内容::

<configuration>
    <fork>true</fork>
    <addResources>true</addResources>
 </configuration>

(三)设置application.properties

spring.devtools.restart.enabled=true

通过以上步骤,就完成了SpringBoot项目的热部署功能

(四)对热部署测试是否成功

1、原来的项目请求。启动项目
http://localhost:8081/cn/hello
2、新加请求,在不重新启动项目的情况下测试热部署是否配置成功~~~

package com.xpwi.springboott;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 杜艳艳
 * @version 1.0
 * @date 2020/7/29 16:07
 */
@RestController
public class HelloController {
    //获取.yml文件中值
   /* @Value("${name}")
    private String name;

    //获取地址
    @Value("${url}")
    private String url;*/

    @Autowired
    private author author;

    //路径映射,对应浏览器访问的地址,访问该路径则执行下面函数
    @RequestMapping("/hello")
    public String hello(){
        return author.getName() + author.getUrl();
    }

    @RequestMapping("/say")
    public String say(){
        return "热部署成功啦";
    }
}

测试新加请求是否成功,浏览器输入http://localhost:8081/cn/say


说明热部署生效

相关文章

网友评论

      本文标题:2、Spring Boot 热部署

      本文链接:https://www.haomeiwen.com/subject/bfwhrktx.html