Hot Swapping

Spring Boot 支持热交换。本部分回答有关其工作原理的问题。

Spring Boot supports hot swapping. This section answers questions about how it works.

Reload Static Content

热加载有几种选项。建议的方法是使用 spring-boot-devtools,因为它提供额外的开发时间特性,例如支持快速应用程序重新启动和 LiveReload 以及合理开发现时间配置(例如,模板缓存)。Devtools 的工作原理是监视类路径以查找更改。这意味着需要对静态资源更改进行“构建”才能使更改生效。在您保存更改时,Eclipse 中默认会自动执行此操作。在 IntelliJ IDEA 中,“创建项目”命令会触发必要的构建。由于 default restart exclusions,对静态资源所做的更改不会触发应用程序的重新启动。但是,它们会触发实时重新加载。

There are several options for hot reloading. The recommended approach is to use spring-boot-devtools, as it provides additional development-time features, such as support for fast application restarts and LiveReload as well as sensible development-time configuration (such as template caching). Devtools works by monitoring the classpath for changes. This means that static resource changes must be "built" for the change to take effect. By default, this happens automatically in Eclipse when you save your changes. In IntelliJ IDEA, the Make Project command triggers the necessary build. Due to the default restart exclusions, changes to static resources do not trigger a restart of your application. They do, however, trigger a live reload.

另外,在 IDE 中运行(尤其是调试模式开启时)是进行开发的一种好方法(所有现代化的 IDE 都允许重新加载静态资源,通常也允许热交换 Java 类更改)。

Alternatively, running in an IDE (especially with debugging on) is a good way to do development (all modern IDEs allow reloading of static resources and usually also allow hot-swapping of Java class changes).

最后,Maven and Gradle plugins 可以进行配置(请参阅 addResources 属性)以支持直接从源代码重新加载静态文件,从而支持从命令行运行。如果您使用更高级别的工具编写代码,则可以将其与外部 css/js 编译器进程一起使用。

Finally, the Maven and Gradle plugins can be configured (see the addResources property) to support running from the command line with reloading of static files directly from source. You can use that with an external css/js compiler process if you are writing that code with higher-level tools.

Reload Templates without Restarting the Container

Spring Boot 支持的大多数模板化技术都包括一个配置选项,用于禁用缓存(稍后在本文档中描述)。如果您使用 spring-boot-devtools 模块,则这些属性在开发时间会自动 automatically configured 给您。

Most of the templating technologies supported by Spring Boot include a configuration option to disable caching (described later in this document). If you use the spring-boot-devtools module, these properties are automatically configured for you at development time.

Thymeleaf Templates

如果您使用 Thymeleaf,请将 spring.thymeleaf.cache 设置为 false。请参阅 {code-spring-boot-autoconfigure-src}/thymeleaf/ThymeleafAutoConfiguration.java[ThymeleafAutoConfiguration] 了解其他 Thymeleaf 自定义选项。

If you use Thymeleaf, set spring.thymeleaf.cache to false. See {code-spring-boot-autoconfigure-src}/thymeleaf/ThymeleafAutoConfiguration.java[ThymeleafAutoConfiguration] for other Thymeleaf customization options.

FreeMarker Templates

如果你使用 FreeMarker,将 spring.freemarker.cache 设置为 false。请参阅 {code-spring-boot-autoconfigure-src}/freemarker/FreeMarkerAutoConfiguration.java[FreeMarkerAutoConfiguration] 了解其他 FreeMarker 自定义选项。

If you use FreeMarker, set spring.freemarker.cache to false. See {code-spring-boot-autoconfigure-src}/freemarker/FreeMarkerAutoConfiguration.java[FreeMarkerAutoConfiguration] for other FreeMarker customization options.

Groovy Templates

如果你使用 Groovy 模板,将 spring.groovy.template.cache 设置为 false。请参阅 {code-spring-boot-autoconfigure-src}/groovy/template/GroovyTemplateAutoConfiguration.java[GroovyTemplateAutoConfiguration] 了解其他 Groovy 自定义选项。

If you use Groovy templates, set spring.groovy.template.cache to false. See {code-spring-boot-autoconfigure-src}/groovy/template/GroovyTemplateAutoConfiguration.java[GroovyTemplateAutoConfiguration] for other Groovy customization options.

Fast Application Restarts

spring-boot-devtools 模块包括自动应用程序重启支持。虽然不像 JRebel 等技术那样快,但通常比 “cold start” 快得多。在研究本文后面讨论的一些更复杂的重新加载选项之前,你应该先尝试一下。

The spring-boot-devtools module includes support for automatic application restarts. While not as fast as technologies such as JRebel it is usually significantly faster than a “cold start”. You should probably give it a try before investigating some of the more complex reload options discussed later in this document.

有关更多详细信息,请参阅 Developer Tools 部分。

For more details, see the Developer Tools section.

Reload Java Classes without Restarting the Container

许多现代 IDE(Eclipse、IDEA 等)支持字节码热交换。因此,如果你进行不影响类或方法签名的更改,则它应能够干净地重新加载,并且没有副作用。

Many modern IDEs (Eclipse, IDEA, and others) support hot swapping of bytecode. Consequently, if you make a change that does not affect class or method signatures, it should reload cleanly with no side effects.