Overview of Spring Boot Dev Tools – Spring Boot开发工具概述

最后修改: 2017年 3月 15日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

Spring Boot gives us the ability to quickly setup and run services.

Spring Boot使我们有能力快速设置和运行服务。

To enhance the development experience further, Spring released the spring-boot-devtools tool – as part of Spring Boot-1.3. This article will try to cover the benefits we can achieve using the new functionality.

为了进一步提升开发体验,Spring发布了spring-boot-devtools工具–作为Spring Boot-1.3的一部分。本文将尝试介绍我们使用新功能可以获得的好处。

We’ll cover the following topics:

我们将涵盖以下主题。

  • Property defaults
  • Automatic Restart
  • Live Reload
  • Global settings
  • Remote applications

1.1. Add Spring-Boot-Devtools in a Project

1.1.在项目中添加Spring-Boot-Devtools

Adding spring-boot-devtools in a project is as simple as adding any other spring-boot module. In an existing spring-boot project, add the following dependency:

在项目中添加spring-boot-devtools和添加任何其他spring-boot模块一样简单。在一个现有的spring-boot项目中,添加以下依赖关系。

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

Do a clean build of the project, and you are now integrated with spring-boot-devtools. The newest version can be fetched from here and all versions can be found here.

对该项目进行清洁构建,现在你已经与spring-boot-devtools集成。最新的版本可以从这里获取,所有的版本可以在这里找到。

2. Property Defaults

2.属性缺省

Spring-boot does a lot of auto-configurations, including enabling caching by default to improve performance. One such example is caching of templates used by template engines, e.g. thymeleaf. But during development, it’s more important to see the changes as quickly as possible.

Spring-boot做了很多自动配置,包括默认启用缓存以提高性能。其中一个例子是对模板引擎使用的模板进行缓存,例如thymeleaf。但在开发过程中,尽可能快地看到变化是更重要的。

The default behavior of caching can be disabled for thymeleaf using the property spring.thymeleaf.cache=false in the application.properties file. We do not need to do this manually, introducing this spring-boot-devtools does this automatically for us.

可以使用application.properties文件中的spring.thymeleaf.cache=false属性来禁用thymeleaf的默认缓存行为。我们不需要手动操作,引入这个spring-boot-devtools会自动为我们做这个。

3. Automatic Restart

3.自动重新启动

In a typical application development environment, a developer would make some changes, build the project and deploy/start the application for new changes to take effect, or else try to leverage JRebel, etc.

在一个典型的应用开发环境中,开发者会做一些改变,构建项目并部署/启动应用,以使新的改变生效,否则会尝试利用JRebel,等等。

Using spring-boot-devtools, this process is also automated. Whenever files change in the classpath, applications using spring-boot-devtools will cause the application to restart. The benefit of this feature is the time required to verify the changes made is considerably reduced:

使用spring-boot-devtools,这个过程也是自动化的。每当classpath中的文件发生变化,使用spring-boot-devtools的应用程序将导致应用程序重新启动。这个功能的好处是,验证所做更改所需的时间大大减少。

19:45:44.804 ... - Included patterns for restart : []
19:45:44.809 ... - Excluded patterns for restart : [/spring-boot-starter/target/classes/, /spring-boot-autoconfigure/target/classes/, /spring-boot-starter-[\w-]+/, /spring-boot/target/classes/, /spring-boot-actuator/target/classes/, /spring-boot-devtools/target/classes/]
19:45:44.810 ... - Matching URLs for reloading : [file:/.../target/test-classes/, file:/.../target/classes/]

 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-03-12 19:45:45.174  ...: Starting Application on machine with PID 7724 (<some path>\target\classes started by user in <project name>)
2017-03-12 19:45:45.175  ...: No active profile set, falling back to default profiles: default
2017-03-12 19:45:45.510  ...: Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@385c3ca3: startup date [Sun Mar 12 19:45:45 IST 2017]; root of context hierarchy

As seen in the logs, the thread that has spawned the application is not a main rather a restartedMain thread. Any changes made in the project be it a java file change will cause an automated restart of the project:

从日志中可以看出,催生应用程序的线程不是main而是restartedMain线程。项目中的任何变化,无论是java文件的变化,都会导致项目的自动重启。

2017-03-12 19:53:46.204  ...: Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@385c3ca3: startup date [Sun Mar 12 19:45:45 IST 2017]; root of context hierarchy
2017-03-12 19:53:46.208  ...: Unregistering JMX-exposed beans on shutdown


 :: Spring Boot ::        (v1.5.2.RELEASE)

2017-03-12 19:53:46.587  ...: Starting Application on machine with PID 7724 (<project path>\target\classes started by user in <project name>)
2017-03-12 19:53:46.588  ...: No active profile set, falling back to default profiles: default
2017-03-12 19:53:46.591  ...: Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@acaf4a1: startup date [Sun Mar 12 19:53:46 IST 2017]; root of context hierarchy

4. Live Reload

4.现场重新加载

spring-boot-devtools module includes an embedded LiveReload server that is used to trigger a browser refresh when a resource is changed.

spring-boot-devtools模块包括一个嵌入式LiveReload服务器,用于在资源发生变化时触发浏览器刷新。

For this to happen in the browser we need to install the LiveReload plugin one such implementation is Remote Live Reload for Chrome.

为了在浏览器中实现这一点,我们需要安装LiveReload插件,其中一个实现是Remote Live Reload for Chrome。

5. Global Settings

5.全局设置

spring-boot-devtools provides a way to configure global settings that are not coupled with any application. This file is named as .spring-boot-devtools.properties and it located at $HOME.

spring-boot-devtools提供了一种配置全局设置的方法,它不与任何应用程序相联系。这个文件被命名为.spring-boot-devtools.properties,它位于$HOME。

6. Remote Applications

6.远程应用

6.1. Remote Debugging via HTTP (Remote Debug Tunnel)

6.1.通过HTTP进行远程调试(远程调试隧道)

spring-boot-devtools provides out of the box remote debugging capabilities via HTTP, to have this feature it is required that spring-boot-devtools are packaged as part of the application. This can be achieved by disabling excludeDevtools configuration in the plugin in maven.

spring-boot-devtools通过HTTP提供开箱即用的远程调试功能,要实现这一功能,需要将spring-boot-devtools打包成应用程序的一部分。这可以通过在maven的插件中禁用excludeDevtools配置来实现。

Here’s a quick sample:

这里有一个快速样本。

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

Now for remote debugging via HTTP to work, following steps have to be taken:

现在要想通过HTTP进行远程调试,必须采取以下步骤。

  1. An application being deployed and started on the server, should be started with Remote Debugging enabled:
    -Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n

    As we can see, the remote debugging port is not mentioned here. Hence, java will choose a random port

    我们可以看到,这里没有提到远程调试的端口。因此,java将随机选择一个端口

  2. For the same project, open the Launch configurations, choose the following options:
    Select main class: org.springframework.boot.devtools.RemoteSpringApplication
    In program arguments, add the URL for the application, e.g. http://localhost:8080
  3. Default port for debugger via spring-boot application is 8000 and can be overridden via:
    spring.devtools.remote.debug.local-port=8010
  4. Now create a remote-debug configuration, setting up the port as 8010 as configured via properties or 8000, if sticking to defaults

Here’s what the log will look like:

下面是日志的样子。

  .   ____          _                                              __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _          ___               _      \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` |        | _ \___ _ __  ___| |_ ___ \ \ \ \
 \\/  ___)| |_)| | | | | || (_| []::::::[]   / -_) '  \/ _ \  _/ -_) ) ) ) )
  '  |____| .__|_| |_|_| |_\__, |        |_|_\___|_|_|_\___/\__\___|/ / / /
 =========|_|==============|___/===================================/_/_/_/
 :: Spring Boot Remote ::  (v1.5.2.RELEASE)

2017-03-12 22:24:11.089  ...: Starting RemoteSpringApplication v1.5.2.RELEASE on machine with PID 10476 (..\org\springframework\boot\spring-boot-devtools\1.5.2.RELEASE\spring-boot-devtools-1.5.2.RELEASE.jar started by user in project)
2017-03-12 22:24:11.097  ...: No active profile set, falling back to default profiles: default
2017-03-12 22:24:11.357  ...: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@11e21d0e: startup date [Sun Mar 12 22:24:11 IST 2017]; root of context hierarchy
2017-03-12 22:24:11.869  ...: The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
2017-03-12 22:24:11.949  ...: LiveReload server is running on port 35729
2017-03-12 22:24:11.983  ...: Started RemoteSpringApplication in 1.24 seconds (JVM running for 1.802)
2017-03-12 22:24:34.324  ...: Remote debug connection opened

6.2. Remote Update

6.2.远程更新

The remote client monitors the application classpath for changes as is done for remote restart feature. Any change in the classpath causes, the updated resource to be pushed to the remote application and a restart is triggered.

远程客户端监控应用程序classpath的变化,正如远程重启功能所做的那样。classpath的任何变化都会导致更新的资源被推送到远程应用程序,并触发重启。

Changes are pushed when the remote client is up and running, as monitoring for changed files is only possible then.

更改是在远程客户端启动和运行时推送的,因为只有在那时才可能监测到更改的文件。

Here’s what that looks like in the logs:

以下是日志中的情况。

2017-03-12 22:33:11.613  INFO 1484 ...: Remote debug connection opened
2017-03-12 22:33:21.869  INFO 1484 ...: Uploaded 1 class resource

7. Conclusion

7.结论

With this quick article, we have just demonstrated how we can leverage the spring-boot-devtools module to make the developer experience better and reduce the development time by automating a lot of activities.

通过这篇简短的文章,我们刚刚展示了我们如何利用spring-boot-devtools模块来使开发者的体验更好,并通过自动化大量的活动来减少开发时间。