java - 实时重新加载在 Spring boot devtools 中不起作用

标签 java spring spring-mvc spring-boot

我在 spring boot 应用程序中使用此依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional>
</dependency>

documentation说:

By default, changing resources in /META-INF/maven, /META-INF/resources, /resources, /static, /public, or /templates does not trigger a restart but does trigger a live reload.

live reload documentation说:

The spring-boot-devtools module includes an embedded LiveReload server that can be used to trigger a browser refresh when a resource is changed. LiveReload browser extensions are freely available for Chrome, Firefox and Safari from livereload.com.

现在,我正在使用 Maven,我的静态文件夹在 src/main/resources 下,所以我的文件夹结构是:

src/main/resources/static/index.html

这是我的 index.html 文件中的内容:

<!DOCTYPE HTML>
<html>
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p>HI THERE! Get your greeting <a href="/greeting">here</a></p>
</body>
</html>

我使用 chrome 浏览器,但没有在 chrome 中使用任何实时重新加载浏览器扩展程序。

我使用这个 Powershell 命令运行应用程序(因为我使用的是 spring boot maven 插件):

mvn clean package; java -jar target\project-name-version.jar

这会在 localhost:8080 上启动服务器并在网页中显示 index.html 的内容。

但是,当我随后在 Eclipse IDE 中更改 index.html 并保存文件,然后刷新浏览器页面时,我没有看到新的更改。

为什么实时重新加载对我不起作用?我做错了什么?

最佳答案

从 IDE 运行应用程序并不是使 Developer 工具正常工作的约束。 您的问题出在其他地方。

这些命令:

mvn clean package; 
java -jar target\project-name-version.jar

意味着您不使用 spring-boot maven 插件来运行您的应用程序。
您运行完全打包的应用程序的可自动启动的 jar。 因此,如 documentation 所述,Spring Boot devtools 被禁用。 :

Developer tools are automatically disabled when running a fully packaged application. If your application is launched from java -jar or if it is started from a special classloader, then it is considered a “production application”. Flagging the dependency as optional is a best practice that prevents devtools from being transitively applied to other modules that use your project. Gradle does not support optional dependencies out-of-the-box, so you may want to have a look at the propdeps-plugin.

要在 exploded/dev 模式下从命令行运行应用程序,mvn clean package 不是必需的,也很无奈。
只需执行 mvn spring-boot:run

关于java - 实时重新加载在 Spring boot devtools 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49386434/

相关文章:

java - Akka Java OneForOneStrategy 示例未编译

java - Eclipse 类型建议从 3.4 更改为 3.5 - 可配置?

java - 幸运数字与否

java - 应用程序仅在 Samsung Galaxy S7 Edge 上速度缓慢并出现 OutOfMemoryException

java - 没有可用类型的合格 bean : expected at least 1 bean which qualifies as autowire candidate

java - 使用 StoredProcedureQuery 时 session /EntityManager 关闭

java - Spring单例初始化完成后如何运行方法?

java - 数据未持久保存到数据库并在控制台中显示空指针异常

spring-mvc - 如何配置 MultipartResolver?

java - 同一 Controller Spring MVC 中的多个用户