最近在 IntelliJ 中使用 spring-boot-devtools 启动了 spring-boot,并花了几个小时试图弄清楚为什么 IntelliJ 不会接受我的更改并自动重启嵌入式 tomcat。
此链接上的信息也无济于事:https ://dzone.com/articles/spring-boot-application-live-reload-hot-swap-with
最近在 IntelliJ 中使用 spring-boot-devtools 启动了 spring-boot,并花了几个小时试图弄清楚为什么 IntelliJ 不会接受我的更改并自动重启嵌入式 tomcat。
此链接上的信息也无济于事:https ://dzone.com/articles/spring-boot-application-live-reload-hot-swap-with
为了使其工作,您需要:
1)在 maven 或 gradle 中启用 devtools。在 Maven 中,它看起来像:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope><!-- -->
<optional>true</optional>
</dependency>
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 的参数使用引号。如果不这样做,项目将第一次运行,然后在重新启动时失败。
spring:
profiles:
active: '@spring.profiles.active@'
它适用于 INTELLIJIDEA 社区版,值得大写,因为许多答案说它只适用于终极版......
通过将项目名称从 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=
希望这可以帮助某人节省时间。