Developer Tools

Spring Boot 包含了另外一组可以使应用程序开发体验更愉快的工具。任何项目都可以包含`spring-boot-devtools`模块,以提供额外的开发时间功能。如以下针对 Maven 和 Gradle 的清单所示,要包含 devtools 支持,请将模块依赖项添加到您的构建中:

Spring Boot includes an additional set of tools that can make the application development experience a little more pleasant. The spring-boot-devtools module can be included in any project to provide additional development-time features. To include devtools support, add the module dependency to your build, as shown in the following listings for Maven and Gradle:

Maven
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency>
</dependencies>
Gradle
dependencies {
	developmentOnly("org.springframework.boot:spring-boot-devtools")
}

Devtools 可能会导致类加载问题,特别是在多模块项目中。Diagnosing Classloading Issues说明了如何诊断和解决它们。

Devtools might cause classloading issues, in particular in multi-module projects. Diagnosing Classloading Issues explains how to diagnose and solve them.

在运行完全打包的应用程序时,开发人员工具会自动禁用。如果您的应用程序从`java -jar`启动,或者从特殊类加载器启动,则它被视为"`production application`"。您可以使用`spring.devtools.restart.enabled`系统属性来控制此行为。要启用 devtools,无论用来启动应用程序的类加载器如何,请设置`-Dspring.devtools.restart.enabled=true`系统属性。不能在生产环境中执行此操作,因为在此环境下运行 devtools 是一个安全风险。要禁用 devtools,请排除依赖项或设置 -Dspring.devtools.restart.enabled=false 系统属性。

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”. You can control this behavior by using the spring.devtools.restart.enabled system property. To enable devtools, irrespective of the classloader used to launch your application, set the -Dspring.devtools.restart.enabled=true system property. This must not be done in a production environment where running devtools is a security risk. To disable devtools, exclude the dependency or set the -Dspring.devtools.restart.enabled=false system property.

以可选形式在 Maven 中标记依赖项或使用 Gradle 中的`developmentOnly`配置(如上所示),可以防止 devtools 传递性地应用于使用您的项目的其他模块。

Flagging the dependency as optional in Maven or using the developmentOnly configuration in Gradle (as shown above) prevents devtools from being transitively applied to other modules that use your project.

重新打包的存档默认情况下不包含 devtools。如果您想使用 certain remote devtools feature,您需要包括它。使用 Maven 插件时,将 excludeDevtools 属性设置为 false。使用 Gradle 插件时,configure the task’s classpath to include the developmentOnly configuration

Repackaged archives do not contain devtools by default. If you want to use a certain remote devtools feature, you need to include it. When using the Maven plugin, set the excludeDevtools property to false. When using the Gradle plugin, configure the task’s classpath to include the developmentOnly configuration.

Diagnosing Classloading Issues

正如 Restart vs Reload 部分中所述,重新启动功能是通过使用两个类加载器实现的。对于大多数应用程序,此方法运行良好。但是,它有时会导致类加载问题,特别是在多模块项目中。

As described in the Restart vs Reload section, restart functionality is implemented by using two classloaders. For most applications, this approach works well. However, it can sometimes cause classloading issues, in particular in multi-module projects.

若要诊断类加载问题实际上是由 devtools 及其两个类加载器导致的,try disabling restart。如果这解决了您的问题,customize the restart classloader 以包含您的整个项目。

To diagnose whether the classloading issues are indeed caused by devtools and its two classloaders, try disabling restart. If this solves your problems, customize the restart classloader to include your entire project.

Property Defaults

Spring Boot 支持的几个库使用缓存来提高性能。例如,template engines 缓存编译的模板以避免重复解析模板文件。此外,Spring MVC 可以在提供静态资源时为响应添加 HTTP 缓存标头。

Several of the libraries supported by Spring Boot use caches to improve performance. For example, template engines cache compiled templates to avoid repeatedly parsing template files. Also, Spring MVC can add HTTP caching headers to responses when serving static resources.

虽然缓存对于生产非常有益,但它在开发过程中可能适得其反,从而阻止您看到您刚刚在应用程序中所做的更改。出于此原因,spring-boot-devtools 默认禁用缓存选项。

While caching is very beneficial in production, it can be counter-productive during development, preventing you from seeing the changes you just made in your application. For this reason, spring-boot-devtools disables the caching options by default.

缓存选项通常由 application.properties 文件中的设置配置。例如,Thymeleaf 提供 configprop:spring.thymeleaf.cache[] 属性。spring-boot-devtools 模块会自动应用合理的开发时间配置,而无需手动设置这些属性。

Cache options are usually configured by settings in your application.properties file. For example, Thymeleaf offers the configprop:spring.thymeleaf.cache[] property. Rather than needing to set these properties manually, the spring-boot-devtools module automatically applies sensible development-time configuration.

下表列出了应用的所有属性:

The following table lists all the properties that are applied:

Unresolved include directive in modules/reference/pages/using/devtools.adoc - include::ROOT:partial$propertydefaults/devtools-property-defaults.adoc[]

如果您不希望应用属性默认值,您可以在 application.properties 中将 configprop:spring.devtools.add-properties[] 设置为 false

If you do not want property defaults to be applied you can set configprop:spring.devtools.add-properties[] to false in your application.properties.

由于您在开发 Spring MVC 和 Spring WebFlux 应用程序时需要更多有关网络请求的信息,开发人员工具建议您为 web 日志组启用 DEBUG 日志记录。这将为您提供有关传入请求、哪个处理程序正在处理它、响应结果和其他详细信息的信息。如果您希望记录所有请求详细信息(包括可能包含敏感信息),则可以打开 configprop:spring.mvc.log-request-details[] 或 configprop:spring.codec.log-request-details[] 配置属性。

Because you need more information about web requests while developing Spring MVC and Spring WebFlux applications, developer tools suggests you to enable DEBUG logging for the web logging group. This will give you information about the incoming request, which handler is processing it, the response outcome, and other details. If you wish to log all request details (including potentially sensitive information), you can turn on the configprop:spring.mvc.log-request-details[] or configprop:spring.codec.log-request-details[] configuration properties.

Automatic Restart

使用 spring-boot-devtools 的应用程序会在类路径上的文件发生更改时自动重新启动。由于这为代码更改提供了非常快的反馈环路,因此当在 IDE 中工作时,这可能是一个有用的功能。默认情况下,指向目录的类路径上的任何条目都将受到更改的监视。请注意,诸如静态资源和视图模板等特定资源,do not need to restart the application

Applications that use spring-boot-devtools automatically restart whenever files on the classpath change. This can be a useful feature when working in an IDE, as it gives a very fast feedback loop for code changes. By default, any entry on the classpath that points to a directory is monitored for changes. Note that certain resources, such as static assets and view templates, do not need to restart the application.

Triggering a restart

由于 DevTools 监视类路径资源,因此触发重启的唯一方法是更新类路径。无论您使用的是 IDE 还是其中一个构建插件,都必须重新编译已修改的文件才能触发重启。导致类路径更新的方式取决于您正在使用的工具:

As DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. Whether you’re using an IDE or one of the build plugins, the modified files have to be recompiled to trigger a restart. The way in which you cause the classpath to be updated depends on the tool that you are using:

  • In Eclipse, saving a modified file causes the classpath to be updated and triggers a restart.

  • In IntelliJ IDEA, building the project (Build → Build Project) has the same effect.

  • If using a build plugin, running mvn compile for Maven or gradle build for Gradle will trigger a restart.

如果您使用构建插件通过 Maven 或 Gradle 重新启动,则必须将 forking 设置为 enabled。如果您禁用派生,devtools 使用的孤立应用程序类加载器将不会被创建,并且重启将无法正常运行。

If you are restarting with Maven or Gradle using the build plugin you must leave the forking set to enabled. If you disable forking, the isolated application classloader used by devtools will not be created and restarts will not operate properly.

当与 LiveReload 一起使用时,自动重启效果很好。See the LiveReload section 有关详细信息。如果您使用 JRebel,则为了动态类重新加载而禁用了自动重启。其他 devtools 功能(例如 LiveReload 和属性覆盖)仍然可以使用。

Automatic restart works very well when used with LiveReload. See the LiveReload section for details. If you use JRebel, automatic restarts are disabled in favor of dynamic class reloading. Other devtools features (such as LiveReload and property overrides) can still be used.

DevTools 依赖于应用程序上下文的关闭钩子在重启期间关闭它。如果您禁用了关闭钩子 (SpringApplication.setRegisterShutdownHook(false)),它将无法正常工作。

DevTools relies on the application context’s shutdown hook to close it during a restart. It does not work correctly if you have disabled the shutdown hook (SpringApplication.setRegisterShutdownHook(false)).

DevTools 需要自定义 ApplicationContext 使用的 ResourceLoader。如果您的应用程序已经提供了一个,它将被包装。不支持直接覆盖 ApplicationContextgetResource 方法。

DevTools needs to customize the ResourceLoader used by the ApplicationContext. If your application provides one already, it is going to be wrapped. Direct override of the getResource method on the ApplicationContext is not supported.

使用 AspectJ 织入时不支持自动重新启动。

Automatic restart is not supported when using AspectJ weaving.

Restart vs Reload

Spring Boot 提供的重新启动技术使用两个类加载器。不会更改的类(例如,来自第三方文件 jar 的类)将加载到 base 类加载器中。正在积极开发的类将加载到 restart 类加载器中。在重新启动应用程序时,将丢弃 restart 类加载器并创建一个新的类加载器。这种方法意味着应用程序的重新启动通常比 “cold starts” 快得多,因为 base 类加载器已可用且已填充。

The restart technology provided by Spring Boot works by using two classloaders. Classes that do not change (for example, those from third-party jars) are loaded into a base classloader. Classes that you are actively developing are loaded into a restart classloader. When the application is restarted, the restart classloader is thrown away and a new one is created. This approach means that application restarts are typically much faster than “cold starts”, since the base classloader is already available and populated.

如果你发现你的应用程序的重新启动速度不够快,或遇到类加载问题,可以考虑使用 ZeroTurnaround 的 JRebel 等重新加载技术。它们通过以更适合重新加载的方式重写加载的类来工作。

If you find that restarts are not quick enough for your applications or you encounter classloading issues, you could consider reloading technologies such as JRebel from ZeroTurnaround. These work by rewriting classes as they are loaded to make them more amenable to reloading.

Logging Changes in Condition Evaluation

默认情况下,每次你的应用程序重新启动时,都会记录一份显示条件评估增量差的报告。该报告显示你的应用程序的自动配置的更改,例如添加或删除 Bean 以及设置配置属性。

By default, each time your application restarts, a report showing the condition evaluation delta is logged. The report shows the changes to your application’s auto-configuration as you make changes such as adding or removing beans and setting configuration properties.

若要禁用报告的记录,请设置以下属性:

To disable the logging of the report, set the following property:

spring:
  devtools:
    restart:
      log-condition-evaluation-delta: false

Excluding Resources

某些资源不一定需要在更改时触发重新启动。例如,Thymeleaf 模板可以就地编辑。默认情况下,在 /META-INF/maven, /META-INF/resources, /resources, /static, /public/templates 中更改资源不会触发重新启动,只会触发 live reload。如果你想自定义这些排除项,可以使用 configprop:spring.devtools.restart.exclude[] 属性。例如,若只想排除 /static/public,则需设置以下属性:

Certain resources do not necessarily need to trigger a restart when they are changed. For example, Thymeleaf templates can be edited in-place. 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. If you want to customize these exclusions, you can use the configprop:spring.devtools.restart.exclude[] property. For example, to exclude only /static and /public you would set the following property:

spring:
  devtools:
    restart:
      exclude: "static/**,public/**"

如果你想保留这些默认设置并 add 其他排除项,请改用 configprop:spring.devtools.restart.additional-exclude[] 属性。

If you want to keep those defaults and add additional exclusions, use the configprop:spring.devtools.restart.additional-exclude[] property instead.

Watching Additional Paths

你可能希望在对不在类路径上的文件进行更改时重新启动或重新加载你的应用程序。为此,请使用 configprop:spring.devtools.restart.additional-paths[] 属性来配置要监视的用于更改的其他路径。可以使用 configprop:spring.devtools.restart.exclude[] 属性 described earlier 控制其他路径下的更改是触发完全重新启动还是 live reload

You may want your application to be restarted or reloaded when you make changes to files that are not on the classpath. To do so, use the configprop:spring.devtools.restart.additional-paths[] property to configure additional paths to watch for changes. You can use the configprop:spring.devtools.restart.exclude[] property described earlier to control whether changes beneath the additional paths trigger a full restart or a live reload.

Disabling Restart

如果你不想使用重新启动功能,可以使用 configprop:spring.devtools.restart.enabled[] 属性将其禁用。在大多数情况下,你可以在 application.properties 中设置此属性(这样做仍然会初始化重新启动类加载器,但不会监视文件更改)。

If you do not want to use the restart feature, you can disable it by using the configprop:spring.devtools.restart.enabled[] property. In most cases, you can set this property in your application.properties (doing so still initializes the restart classloader, but it does not watch for file changes).

如果你需要 completely 禁用重新启动支持(例如,因为它不适用于特定库),你需要在调用 SpringApplication.run(…​) 之前将 configprop:spring.devtools.restart.enabled[] System 属性设置为 false,如下例所示:

If you need to completely disable restart support (for example, because it does not work with a specific library), you need to set the configprop:spring.devtools.restart.enabled[] System property to false before calling SpringApplication.run(…​), as shown in the following example:

Using a Trigger File

如果你使用的是不断编译已更改文件的 IDE,你可能更喜欢只在特定时间触发重新启动。为此,可以使用 “trigger file”,该 “trigger file” 是一个特殊文件,你需要在想要实际触发重新启动检查时对其进行修改。

If you work with an IDE that continuously compiles changed files, you might prefer to trigger restarts only at specific times. To do so, you can use a “trigger file”, which is a special file that must be modified when you want to actually trigger a restart check.

对该文件的任何更新都将触发检查,但仅在 Devtools 检测到其有任务要做的前提下才会真正重新启动。

Any update to the file will trigger a check, but restart only actually occurs if Devtools has detected it has something to do.

若要使用触发器文件,请将 configprop:spring.devtools.restart.trigger-file[] 属性设置为触发器文件的名称(不包括任何路径)。触发器文件必须出现在你的类路径的某个位置。

To use a trigger file, set the configprop:spring.devtools.restart.trigger-file[] property to the name (excluding any path) of your trigger file. The trigger file must appear somewhere on your classpath.

例如,如果你有一个如下结构的项目:

For example, if you have a project with the following structure:

src
+- main
   +- resources
      +- .reloadtrigger

则你的 trigger-file 属性将为:

Then your trigger-file property would be:

spring:
  devtools:
    restart:
      trigger-file: ".reloadtrigger"

现在只有在 src/main/resources/.reloadtrigger 更新时才会重新启动。

Restarts will now only happen when the src/main/resources/.reloadtrigger is updated.

你可能希望将 spring.devtools.restart.trigger-file 设置为 global setting,以便你的所有项目都以相同的方式运行。

You might want to set spring.devtools.restart.trigger-file as a global setting, so that all your projects behave in the same way.

有些 IDE 具有无需手动更新触发器文件就能自动保存的特性。 Spring Tools for EclipseIntelliJ IDEA (Ultimate Edition) 都具有此类支持。对于 Spring Tools,可以使用控制台视图中的 “reload” 按钮(只要你的 trigger-file 命名为 .reloadtrigger)。对于 IntelliJ IDEA,可以按照 instructions in their documentation 的说明进行操作。

Some IDEs have features that save you from needing to update your trigger file manually. Spring Tools for Eclipse and IntelliJ IDEA (Ultimate Edition) both have such support. With Spring Tools, you can use the “reload” button from the console view (as long as your trigger-file is named .reloadtrigger). For IntelliJ IDEA, you can follow the instructions in their documentation.

Customizing the Restart Classloader

Restart vs Reload 部分中前面所述,重新启动功能是通过使用两个类加载器实现的。如果这样会导致问题,你可能需要自定义哪个类加载器加载什么内容。

As described earlier in the Restart vs Reload section, restart functionality is implemented by using two classloaders. If this causes issues, you might need to customize what gets loaded by which classloader.

默认情况下,IDE 中任何打开的项目都会使用 “restart” 类加载器加载,任何常规 .jar 文件都会使用 “base” 类加载器加载。如果您使用 mvn spring-boot:rungradle bootRun,情况也是如此:包含 @SpringBootApplication 的项目使用 “restart” 类加载器加载,其他所有内容都使用 “base” 类加载器加载。

By default, any open project in your IDE is loaded with the “restart” classloader, and any regular .jar file is loaded with the “base” classloader. The same is true if you use mvn spring-boot:run or gradle bootRun: the project containing your @SpringBootApplication is loaded with the “restart” classloader, and everything else with the “base” classloader.

您可以通过创建 META-INF/spring-devtools.properties 文件指示 Spring Boot 使用不同的类加载器加载项目的各个部分。spring-devtools.properties 文件可以包含以 restart.excluderestart.include 为前缀的属性。include 元素应提升到 “restart” 类加载器中的项,exclude 元素应压入到 “base” 类加载器中的项。该属性值是一种正则表达式模式,适用于类路径,如下例所示:

You can instruct Spring Boot to load parts of your project with a different classloader by creating a META-INF/spring-devtools.properties file. The spring-devtools.properties file can contain properties prefixed with restart.exclude and restart.include. The include elements are items that should be pulled up into the “restart” classloader, and the exclude elements are items that should be pushed down into the “base” classloader. The value of the property is a regex pattern that is applied to the classpath, as shown in the following example:

restart:
  exclude:
    companycommonlibs: "/mycorp-common-[\\w\\d-\\.]+\\.jar"
  include:
    projectcommon: "/mycorp-myproj-[\\w\\d-\\.]+\\.jar"

所有属性键必须唯一。只要属性以 restart.include.restart.exclude. 开头,就会考虑它。

All property keys must be unique. As long as a property starts with restart.include. or restart.exclude. it is considered.

来自类路径的所有 META-INF/spring-devtools.properties 都已加载。您可以在项目中或项目使用的库中打包文件。

All META-INF/spring-devtools.properties from the classpath are loaded. You can package files inside your project, or in the libraries that the project consumes.

Known Limitations

重新启动功能不适用于使用标准 ObjectInputStream 反序列化的对象。如果您需要反序列化数据,可能需要将 Spring 的 ConfigurableObjectInputStreamThread.currentThread().getContextClassLoader() 结合使用。

Restart functionality does not work well with objects that are deserialized by using a standard ObjectInputStream. If you need to deserialize data, you may need to use Spring’s ConfigurableObjectInputStream in combination with Thread.currentThread().getContextClassLoader().

不幸的是,一些第三方库在反序列化时没有考虑上下文类加载器。如果您发现此类问题,您需要向原始作者报告。

Unfortunately, several third-party libraries deserialize without considering the context classloader. If you find such a problem, you need to request a fix with the original authors.

LiveReload

spring-boot-devtools 模块包含一个嵌入式 LiveReload 服务器,当资源更改时可以使用该服务器触发浏览器刷新。LiveReload 浏览器扩展程序可免费用于 Chrome、Firefox 和 Safari。您可以在所选浏览器的市场或商店中搜索“LiveReload”来查找这些扩展程序。

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. You can find these extensions by searching 'LiveReload' in the marketplace or store of your chosen browser.

如果您不想在应用程序运行时启动 LiveReload 服务器,则可以将 configprop:spring.devtools.livereload.enabled[] 属性设置为 false

If you do not want to start the LiveReload server when your application runs, you can set the configprop:spring.devtools.livereload.enabled[] property to false.

您一次只能运行一个 LiveReload 服务器。启动应用程序之前,请确保没有其他 LiveReload 服务器正在运行。如果您从 IDE 启动多个应用程序,则只有第一个应用程序有 LiveReload 支持。

You can only run one LiveReload server at a time. Before starting your application, ensure that no other LiveReload servers are running. If you start multiple applications from your IDE, only the first has LiveReload support.

要在文件更改时触发 LiveReload,必须启用 Automatic Restart

To trigger LiveReload when a file changes, Automatic Restart must be enabled.

Global Settings

您可以通过将以下任何文件添加到 $HOME/.config/spring-boot 目录,来配置全局 DevTools 设置:

You can configure global devtools settings by adding any of the following files to the $HOME/.config/spring-boot directory:

  1. spring-boot-devtools.properties

  2. spring-boot-devtools.yaml

  3. spring-boot-devtools.yml

添加到这些文件的任何属性都适用于计算机上使用 DevTools 的 all Spring Boot 应用程序。例如,要配置重新启动始终使用 trigger file,您需要将以下属性添加到 spring-boot-devtools 文件:

Any properties added to these files apply to all Spring Boot applications on your machine that use devtools. For example, to configure restart to always use a trigger file, you would add the following property to your spring-boot-devtools file:

spring:
  devtools:
    restart:
      trigger-file: ".reloadtrigger"

默认情况下,$HOME 是用户的 home 目录。要自定义此位置,请设置 SPRING_DEVTOOLS_HOME 环境变量或 spring.devtools.home 系统属性。

By default, $HOME is the user’s home directory. To customize this location, set the SPRING_DEVTOOLS_HOME environment variable or the spring.devtools.home system property.

如果在 $HOME/.config/spring-boot 中找不到 DevTools 配置文件,则会搜索 $HOME 目录的根目录是否存在 .spring-boot-devtools.properties 文件。这允许您将 DevTools 全局配置与运行在不支持 $HOME/.config/spring-boot 位置的较旧版本 Spring Boot 上的应用程序共享。

If devtools configuration files are not found in $HOME/.config/spring-boot, the root of the $HOME directory is searched for the presence of a .spring-boot-devtools.properties file. This allows you to share the devtools global configuration with applications that are on an older version of Spring Boot that does not support the $HOME/.config/spring-boot location.

配置文件不受 DevTools 属性/yaml 文件支持。

Profiles are not supported in devtools properties/yaml files.

.spring-boot-devtools.properties 中激活的任何配置文件都不会影响 profile-specific configuration files 的加载。YAML 和属性文件中的特定于配置文件的文件名(采用 spring-boot-devtools-<profile>.properties 形式)和 spring.config.activate.on-profile 文档不受支持。

Any profiles activated in .spring-boot-devtools.properties will not affect the loading of profile-specific configuration files. Profile specific filenames (of the form spring-boot-devtools-<profile>.properties) and spring.config.activate.on-profile documents in both YAML and Properties files are not supported.

Configuring File System Watcher

{code-spring-boot-devtools-src}/filewatch/FileSystemWatcher.java[FileSystemWatcher] 通过以特定时间间隔轮询类更改来工作,然后等待预定义的静默期以确保没有更多更改。由于 Spring Boot 完全依赖于 IDE 将文件编译并复制到 Spring Boot 可以读取它们的位置,因此您可能会发现某些更改在 DevTools 重启应用程序时未反映出来。如果您不断观察到此类问题,请尝试将 spring.devtools.restart.poll-intervalspring.devtools.restart.quiet-period 参数增加到适合您的开发环境的值:

{code-spring-boot-devtools-src}/filewatch/FileSystemWatcher.java[FileSystemWatcher] works by polling the class changes with a certain time interval, and then waiting for a predefined quiet period to make sure there are no more changes. Since Spring Boot relies entirely on the IDE to compile and copy files into the location from where Spring Boot can read them, you might find that there are times when certain changes are not reflected when devtools restarts the application. If you observe such problems constantly, try increasing the spring.devtools.restart.poll-interval and spring.devtools.restart.quiet-period parameters to the values that fit your development environment:

spring:
  devtools:
    restart:
      poll-interval: "2s"
      quiet-period: "1s"

现在,每 2 秒就会轮询被监视的类路径目录是否有更改,并维持 1 秒的静默期以确保没有其他类更改。

The monitored classpath directories are now polled every 2 seconds for changes, and a 1 second quiet period is maintained to make sure there are no additional class changes.

Remote Applications

Spring Boot 开发人员工具不仅限于本地开发。您还可以在远程运行应用程序时使用多种功能。远程支持是选择加入的,因为启用它可能存在安全风险。它只能在受信任的网络中运行或在使用 SSL 保护时启用。如果这两个选项都不适合您,则不应使用 DevTools 的远程支持。您永远不应在生产部署中启用支持。

The Spring Boot developer tools are not limited to local development. You can also use several features when running applications remotely. Remote support is opt-in as enabling it can be a security risk. It should only be enabled when running on a trusted network or when secured with SSL. If neither of these options is available to you, you should not use DevTools' remote support. You should never enable support on a production deployment.

要启用它,您需要确保在重新打包后的存档中包含 devtools,如下所示:

To enable it, you need to make sure that devtools is included in the repackaged archive, as shown in the following listing:

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
			<configuration>
				<excludeDevtools>false</excludeDevtools>
			</configuration>
		</plugin>
	</plugins>
</build>

然后,您需要设置 configprop:spring.devtools.remote.secret[] 属性。像任何重要的密码或秘密一样,该值应该唯一且可靠,以致无法猜测或暴力破解。

Then you need to set the configprop:spring.devtools.remote.secret[] property. Like any important password or secret, the value should be unique and strong such that it cannot be guessed or brute-forced.

远程 devtools 支持分为两部分提供:接受连接的服务器端端点和您在 IDE 中运行的客户端应用程序。当 configprop:spring.devtools.remote.secret[] 属性时,会自动启用服务器组件。客户端组件必须手动启动。

Remote devtools support is provided in two parts: a server-side endpoint that accepts connections and a client application that you run in your IDE. The server component is automatically enabled when the configprop:spring.devtools.remote.secret[] property is set. The client component must be launched manually.

不支持 Spring WebFlux 应用程序的远程 devtools。

Remote devtools is not supported for Spring WebFlux applications.

Running the Remote Client Application

远程客户端应用程序设计用于在您的 IDE 中运行。您需要使用与连接到的远程项目相同的类路径运行 org.springframework.boot.devtools.RemoteSpringApplication。该应用程序的唯一必需参数是它连接到的远程 URL。

The remote client application is designed to be run from within your IDE. You need to run org.springframework.boot.devtools.RemoteSpringApplication with the same classpath as the remote project that you connect to. The application’s single required argument is the remote URL to which it connects.

例如,如果您使用 Eclipse 或 Spring Tools,并且已经将名为 my-app 的项目部署到 Cloud Foundry,则您将执行以下操作:

For example, if you are using Eclipse or Spring Tools and you have a project named my-app that you have deployed to Cloud Foundry, you would do the following:

  • Select Run Configurations…​ from the Run menu.

  • Create a new Java Application “launch configuration”.

  • Browse for the my-app project.

  • Use org.springframework.boot.devtools.RemoteSpringApplication as the main class.

  • Add https://myapp.cfapps.io to the Program arguments (or whatever your remote URL is).

正在运行的远程客户端可能如下所示:

A running remote client might resemble the following listing:

Unresolved include directive in modules/reference/pages/using/devtools.adoc - include::ROOT:example$remote-spring-application.txt[]

由于远程客户端使用与实际应用程序相同的类路径,因此它可以直接读取应用程序属性。这就是读取 configprop:spring.devtools.remote.secret[] 属性并将其传递给服务器以进行身份验证的方式。

Because the remote client is using the same classpath as the real application it can directly read application properties. This is how the configprop:spring.devtools.remote.secret[] property is read and passed to the server for authentication.

始终建议将 https:// 用作连接协议,以便对流量进行加密且无法截获密码。

It is always advisable to use https:// as the connection protocol, so that traffic is encrypted and passwords cannot be intercepted.

如果您需要使用代理访问远程应用程序,请配置 spring.devtools.remote.proxy.hostspring.devtools.remote.proxy.port 属性。

If you need to use a proxy to access the remote application, configure the spring.devtools.remote.proxy.host and spring.devtools.remote.proxy.port properties.

Remote Update

远程客户端以与 local restart 相同的方式监视应用程序类路径是否有变化。将任何更新的资源推送到远程应用程序,并且 (if required) 触发重启。如果您正在使用本地没有的云服务的功能,这将很有用。通常,远程更新和重启比完全重建和部署周期快得多。

The remote client monitors your application classpath for changes in the same way as the local restart. Any updated resource is pushed to the remote application and (if required) triggers a restart. This can be helpful if you iterate on a feature that uses a cloud service that you do not have locally. Generally, remote updates and restarts are much quicker than a full rebuild and deploy cycle.

在较慢的开发环境中,可能会发生安静时间不足,并且类中的更改可能会分成批次。在上传第一批类更改后重新启动服务器。由于服务器正在重新启动,因此无法将下一批发送到应用程序。

On a slower development environment, it may happen that the quiet period is not enough, and the changes in the classes may be split into batches. The server is restarted after the first batch of class changes is uploaded. The next batch can’t be sent to the application, since the server is restarting.

这通常由 RemoteSpringApplication 日志中有关无法上传某些类的警告以及随后的重试来体现。但它也可能导致应用程序代码不一致,并在上传第一批更改后无法重新启动。如果您经常观察到此类问题,请尝试将 spring.devtools.restart.poll-intervalspring.devtools.restart.quiet-period 参数增加为您满足您的开发环境的值。请参阅 Configuring File System Watcher 部分来配置这些属性。

This is typically manifested by a warning in the RemoteSpringApplication logs about failing to upload some of the classes, and a consequent retry. But it may also lead to application code inconsistency and failure to restart after the first batch of changes is uploaded. If you observe such problems constantly, try increasing the spring.devtools.restart.poll-interval and spring.devtools.restart.quiet-period parameters to the values that fit your development environment. See the Configuring File System Watcher section for configuring these properties.

只有在远程客户端运行时才监控文件。如果您在启动远程客户端之前更改了文件,则不会将其推送到远程服务器。

Files are only monitored when the remote client is running. If you change a file before starting the remote client, it is not pushed to the remote server.