玖叶教程网

前端编程开发入门

Spring Boot Dev Tools工具(spring-boot-loader-tools)

1. 简介

Spring Boot 使我们能够快速设置和运行服务。为了进一步增强开发体验,Spring 发布了 spring-boot-devtools 工具——作为 Spring Boot-1.3 的一部分。

1.1. 在项目中添加 spring-boot-devtools

在项目中添加 spring-boot-devtools 就像添加任何其他 spring-boot 模块一样简单。在现有的 Spring boot项目中,添加以下依赖项:

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

对项目进行干净的构建,您现在已与 spring-boot-devtools 集成。

2. 属性默认值

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

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

3. 自动重启

在典型的应用程序开发环境中,开发人员会进行一些更改,构建项目并部署/启动应用程序以使新更改生效,或者尝试利用 JRebel 等。

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

2023-07-16 09:45:44.804 ... - Included patterns for restart : []
2023-07-16 09: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/]
2023-07-16 09:45:44.810 ... - Matching URLs for reloading : [file:/.../target/test-classes/, file:/.../target/classes/]

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

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

如日志所示,生成应用程序的线程不是线程,而是重新启动的主线程。项目中所做的任何更改,无论是 java 文件更改,都将导致项目自动重新启动:

2023-07-16 09:53:46.204  ...: Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@385c3ca3: startup date [Sun Jul 16 09:45:45 IST 2023]; root of context hierarchy
2023-07-16 09:53:46.208  ...: Unregistering JMX-exposed beans on shutdown


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

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

4. 实时重新加载

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

为了在浏览器中发生这种情况,我们需要安装 LiveReload 插件,其中一个实现是 Chrome 的远程实时重新加载。

5. 全局设置

spring-boot-devtools 提供了一种配置不与任何应用程序耦合的全局设置的方法。此文件名为 .spring-boot-devtools.properties,位于 $HOME。

6. 远程应用程序

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

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

下面是一个快速示例:

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

现在,要通过HTTP进行远程调试,必须执行以下步骤:

1.在服务器上部署和启动的应用程序应在启用远程调试的情况下启动:

-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n

如我们所见,这里没有提到远程调试端口。因此,java 会选择一个随机端口

2.对于同一项目,打开启动配置,选择以下选项: 选择主类:org.springframework.boot.devtools.RemoteSpringApplication 在程序参数中,添加应用程序的URL,例如 http://localhost:8080

3.通过 spring-boot 应用程序进行调试器的默认端口为 8000,可以通过以下方式覆盖:

spring.devtools.remote.debug.local-port=8010

4.现在创建一个远程调试配置,将端口设置为通过属性配置的 8010 或 8000(如果坚持默认值)

日志如下所示:

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

2023-07-16 10: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)
2023-07-16 10:24:11.097  ...: No active profile set, falling back to default profiles: default
2023-07-16 10:24:11.357  ...: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@11e21d0e: startup date [Sun Jul 16 10:24:11 IST 2023]; root of context hierarchy
2023-07-16 10:24:11.869  ...: The connection to http://localhost:8080 is insecure. You should use a URL starting with 'https://'.
2023-07-16 10:24:11.949  ...: LiveReload server is running on port 35729
2023-07-16 10:24:11.983  ...: Started RemoteSpringApplication in 1.24 seconds (JVM running for 1.802)
2023-07-16 10:24:34.324  ...: Remote debug connection opened

6.2. 远程更新

远程客户端监视应用程序类路径中的更改,就像对远程重新启动功能所做的那样。类路径中的任何更改都会导致将更新的资源推送到远程应用程序并触发重新启动。

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

以下是日志中的外观:

2023-07-16 10:33:11.613  INFO 1484 ...: Remote debug connection opened
2023-07-16 10:33:21.869  INFO 1484 ...: Uploaded 1 class resource

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言