【发布时间】:2015-11-23 10:54:10
【问题描述】:
Spring Boot 1.3 引入了 spring-boot-devtools 以提供与 Spring Reloaded 类似的功能,以重新加载修改后的类并更新 Thymeleaf 模板,而无需重新运行您的应用程序。
我之前一直在使用 Spring Boot 1.2.7(重新加载了 Spring),我能够即时修改我的模板,而无需重新启动我的 Spring Boot 应用程序。
当我修改和保存 Java 代码/Thymeleaf 模板时,同一个应用程序现在既不会重新加载 Thymeleaf 模板,也不会重新加载/重新启动应用程序。
我正在使用 Netbeans IDE 中嵌入的 Netbeans 8.0.2 和 Maven(版本 3.0.5)。应用程序被打包为 JAR。
在 Netbeans 中,在项目 Properties -> Build -> Compile 下是一个复选框“Compile On Save”,它被选中。 我通过修改 .java 文件并检查 /target/classes 中的时间戳来验证这确实有效。
这里是 "Run action" properties for the project in Netbeans:
我的 pom.xml 中有以下依赖项(包括其他,因不相关而被排除):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
有了这个,我应该准备好了,因为 Spring Boot 博客提到以下内容:
“当您包含 spring-boot-devtools 模块时,任何类路径文件更改都会自动触发应用程序重启。”
类似的言论在Spring Boot official documentation.
编辑: 我尝试使用带有版本标签 1.2.7.RELEASE 的 spring-boot-maven-plugin 并且在保存模板时突然更改我的 Thymeleaf 模板在浏览器中可见。看来至少 Thymeleaf 模板的问题不是因为 spring-boot-devtools,而是因为 spring-bot-maven-plugin。
问题可以分为两部分:
1) 如果使用较新版本的 spring-boot-maven-plugin (1.3.0.RELEASE),Thymeleaf 模板由于某种原因不会重新加载 2) 应用程序重新加载/重启触发器不会发生,即使 /target/classes 中的 .class 文件在相应的 .java 文件被修改和保存时得到更新。
更新:验证没有加载开发工具(主线程名称未重新启动Main)。 解决 2) 通过将 Netbeans 项目属性中的 Run project Action 中的 Execute 目标更改为以下内容:
process-classes org.codehaus.mojo:exec-maven-plugin:1.2.1:exec
旧的执行目标是package spring-boot:run。谷歌搜索了一下,当项目使用 spring-boot:run 运行时,其他人发现 spring-boot-devtools 存在问题。
现在唯一的问题是 Thymeleaf 模板在保存时不会实时更新。
【问题讨论】:
-
"任何类路径文件更改"
-
@StéphaneNicoll:Netbeans 正在编译保存 Java 源代码时需要更新的 .class 文件。由于您提示检查主线程名称,我能够验证 devtools 没有运行 - 谢谢!
-
那你能回答你自己的问题吗?谢谢。
-
Thymeleaf 模板没有被“实时更新”仍然存在问题。
标签: spring-boot thymeleaf netbeans-8