Spring Boot实现热部署的实例方法

作者:angryTom 时间:2023-10-24 08:12:50 

Spring Boot怎么实现热部署

在Spring Boot实现代码热部署是一件很简单的事情,代码的修改可以自动部署并重新热启动项目。

1、引用devtools依赖


<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-devtools</artifactId>

<optional>true</optional>

</dependency>

这样,当修改一个java类时就会热更新。

2、自定义配置热部署

以下配置用于自定义配置热部署,可以不设置。


# 热部署开关,false即不启用热部署

spring.devtools.restart.enabled: true

# 指定热部署的目录

#spring.devtools.restart.additional-paths: src/main/java

# 指定目录不更新

spring.devtools.restart.exclude: test/**

3、Intellij Idea修改

如果是idea,需要改以下两个地方:

1、勾上自动编译或者手动重新编译

File > Settings > Compiler-Build Project automatically

2、注册

ctrl + shift + alt + / > Registry > 勾选Compiler autoMake allow when app running

注意事项

1、生产环境devtools将被禁用,如java -jar方式或者自定义的类加载器等都会识别为生产环境。

2、打包应用默认不会包含devtools,除非你禁用SpringBoot Maven插件的excludeDevtools属性。

3、Thymeleaf无需配置spring.thymeleaf.cache: false,devtools默认会自动设置,点击参考完整属性。

下面是devtools自动配置的部分源码:


@Order(Ordered.LOWEST_PRECEDENCE)

public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostProcessor {

private static final Map<String, Object> PROPERTIES;

static {

Map<String, Object> properties = new HashMap<String, Object>();

properties.put("spring.thymeleaf.cache", "false");

properties.put("spring.freemarker.cache", "false");

properties.put("spring.groovy.template.cache", "false");

properties.put("spring.mustache.cache", "false");

properties.put("server.session.persistent", "true");

properties.put("spring.h2.console.enabled", "true");

properties.put("spring.resources.cache-period", "0");

properties.put("spring.resources.chain.cache", "false");

properties.put("spring.template.provider.cache", "false");

properties.put("spring.mvc.log-resolved-exception", "true");

properties.put("server.jsp-servlet.init-parameters.development", "true");

PROPERTIES = Collections.unmodifiableMap(properties);

}

4、devtools会在windows资源管理器占用java进程,在开发工具里面杀不掉,只能手动kill掉,不然重启会选成端口重复绑定报错。

来源:https://www.php.cn/java-article-442728.html

标签:Spring,Boot,热部署
0
投稿

猜你喜欢

  • MyBatis动态SQL中的trim标签的使用方法

    2022-08-30 21:38:13
  • android工程下不能运行java main程序的解决方法

    2023-06-23 21:54:08
  • 基于Java8实现提高Excel读写效率

    2023-11-25 10:01:37
  • Android Studio设置主题与字体大小图文教程

    2023-05-25 15:54:10
  • Java受检异常的一些思考

    2021-06-08 08:22:16
  • 使用okhttp替换Feign默认Client的操作

    2021-10-03 16:57:59
  • Android编程实现wifi扫描及连接的方法

    2022-11-21 21:11:36
  • Java NIO Path接口和Files类配合操作文件的实例

    2023-10-20 09:29:01
  • Java加载property文件配置过程解析

    2023-10-07 07:53:03
  • Android Studio设置或修改Android SDK路径方法

    2023-11-07 17:22:10
  • Filter过滤器和Listener监听器详解

    2021-08-20 07:39:38
  • 从零开始Java实现Parser Combinator

    2023-06-18 18:52:04
  • C#使用struct类型作为泛型Dictionary<TKey,TValue>的键

    2023-10-09 01:53:35
  • android同时控制EditText输入字符个数和禁止特殊字符输入的方法

    2021-08-01 14:16:46
  • java多线程之线程同步七种方式代码示例

    2022-04-29 17:55:46
  • C#单例类的实现方法

    2023-12-01 02:13:30
  • 基于Scala和Java方法的相互调用

    2021-07-05 11:53:04
  • Android如何实现锁屏状态下弹窗

    2021-05-27 03:38:41
  • 有关微博content的封装实现详解

    2022-12-02 17:37:53
  • 详解C#如何实现读写ini文件

    2022-02-04 23:15:27
  • asp之家 软件编程 m.aspxhome.com