Spring Boot 开发人员工具自动重启在 IntelliJ 中不起作用

Sam*_*aik 8 java spring intellij-idea spring-boot spring-boot-devtools

最近在 IntelliJ 中使用 spring-boot-devtools 启动了 spring-boot,并花了几个小时试图弄清楚为什么 IntelliJ 不会接受我的更改并自动重启嵌入式 tomcat。

此链接中的信息也没有帮助:https : //dzone.com/articles/spring-boot-application-live-reload-hot-swap-with

小智 7

为了使其工作,您需要:

1) 在 maven 或 gradle 中启用 devtools。在Maven中它看起来像:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
         <scope>runtime</scope><!-- -->
        <optional>true</optional>
    </dependency>
Run Code Online (Sandbox Code Playgroud)

2)在IntellijIDEA中:进入设置(ctrl +alt+s)->构建、执行、部署->编译器,勾选“自动构建项目”

3)在 IntellijIDEA 中:按 ctrl+shift+a,然后输入“registry”并单击它。然后启用选项“compiler.automake.allow.when.app.running”。

4)重新启动intellijIDEA!因为这个,我失去了几个小时:/

它现在应该可以工作了。

请注意:

- 您不需要附加路径或触发器文件即可按预期工作。

- 如果您使用 maven 启动参数-Dspring-boot.run.fork=false来启用调试,则 devtools 将被禁用,因此不应在代码更改时重新启动。

- 在 yaml 文件中,您需要对来自 pom.xml 的参数使用引号。如果不这样做,该项目将第一次运行,然后在重新启动时失败。

spring:
  profiles:
    active: '@spring.profiles.active@'
Run Code Online (Sandbox Code Playgroud)

它适用于 INTELLIJIDEA 社区版,值得大写,因为许多答案说它只适用于终极...

  • 很好的答案,但“compiler.automake.allow.when.app.running”选项已移至高级设置,“即使开发的应用程序当前正在运行,也允许自动启动”。检查添加依赖项后是否应该有效。 (3认同)

Sam*_*aik 6

通过将项目名称从 spring-boot 更改为 spring-boot-xxx (基本上除了 spring-boot 之外的任何名称)解决了该问题。

如果您仔细阅读文档,这里会提到以下内容:

当决定类路径上的条目发生更改时是否应触发重新启动时,DevTools 会自动忽略名为spring-boot、 spring-boot-devtools 、 spring-boot-autoconfigure 、 spring-boot-actuator 和 spring-boot-starter 的项目。

使用 Ctrl+F9 构建项目会自动触发重新启动。如果您希望在保存类文件后​​立即自动触发,您可以按照问题中提供的热插拔链接进行操作。

Spring Boot 还可以选择在特定文件更改时触发重新启动,并且可以使用以下属性在 application.properties 中进行配置

spring.devtools.restart.trigger-file=

希望这可以帮助某人节省时间。