Java Spring Boot 中的热交换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21399586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:28:53  来源:igfitidea点击:

Hot swapping in Spring Boot

javaeclipsespringintellij-ideaspring-boot

提问by Amnon

I've been doing a P.O.C with Spring Boot.

我一直在用 Spring Boot 做 POC。

So far it's been going really good and promising, but there's one major drawback: I'm using an embedded server (i.e., packaging the web app in a .jar), so when developing I have to rebuild the jar and restart the server every time I change the CSS, HTML or JS files. There's not hot-swap. This really slows down the UI development.

到目前为止,它已经持续非常好,有前途的,但有一个主要的缺点:我使用的是嵌入式服务器(即包装的Web应用程序的.jar)开发我的时候,所以不得不重建罐子,然后重新启动服务器每次我更改 CSS、HTML 或 JS 文件。没有热插拔。这确实减慢了 UI 的开发速度。

I can think of several quick fixes, such as loading static resources off a different domain and serving it from a local nginx, and some more variations like this, but isn't there a built-in option of some sort when working with IntelliJ/Eclipse?

我可以想到几个快速修复,例如从不同的域加载静态资源并从本地提供它nginx,以及更多类似的变体,但是在使用 IntelliJ/Eclipse 时没有某种内置选项?

回答by Dave Syer

There are several options. 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 hotswapping of Java class changes). Spring Boot devtools is a cheap way to get quite a big boost (just add it to your classpath). It works by restarting your application in a hot JVM when changes are detected. It also switches off things like thymeleaf caches while it is running, so you don't have to remember to do that yourself. You can use it with an external css/js compiler process if you are writing that code with higher level tools.

有几种选择。在 IDE 中运行(尤其是在调试时)是进行开发的好方法(所有现代 IDE 都允许重新加载静态资源,通常还允许热交换 Java 类更改)。Spring Boot devtools 是一种获得相当大提升的廉价方法(只需将其添加到您的类路径中)。它的工作原理是在检测到更改时在热 JVM 中重新启动应用程序。它还会在运行时关闭 thymeleaf 缓存之类的东西,因此您不必记住自己这样做。如果您使用更高级别的工具编写该代码,则可以将其与外部 css/js 编译器进程一起使用。

Spring Loadedis no longer recommended, but probably still in use. More sophisticated agent-based tools work much better if you need hot swapping with zero delay (e.g. JRebel).

不再推荐使用Spring Loaded,但可能仍在使用。如果您需要零延迟热交换(例如 JRebel),则更复杂的基于代理的工具会更好地工作。

See the docsfor some up to date content

有关一些最新内容,请参阅文档

回答by Jakub Kubrynski

You can also use JRebel - it will reload all changes (better hotswap) including spring beans. It is easily integratred with both Intellij and Eclipse.

您还可以使用 JRebel - 它会重新加载所有更改(更好的热交换),包括 spring bean。它很容易与 Intellij 和 Eclipse 集成。

回答by astronauthere

Assuming you are using gradle; use the following config in your build.gradle

假设您使用的是 gradle;在 build.gradle 中使用以下配置

apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'application'

applicationDefaultJvmArgs = ["-agentlib:jdwp=transport=dt_socket,address=localhost:7000,server=y,suspend=n"]

mainClassName = "package.ApplicationRunner"

Run the application from the IDE or command line using the command gradle build run

使用命令从 IDE 或命令行运行应用程序 gradle build run

Now the IDE can connect to the remote JVM (on port 7000) where the spring boot application runs. It also supports hot deployment of static files.

现在,IDE 可以连接到运行 spring boot 应用程序的远程 JVM(在端口 7000 上)。它还支持静态文件的热部署。

or even you can run the main class from intelliJ if the dependencies are properly managed in the IDE. The main class is the class that contains the main method which will call SpringApplication.run("classpath:/applicationContext.xml", args);

或者,如果在 IDE 中正确管理依赖项,您甚至可以从 intelliJ 运行主类。主类是包含将调用的主要方法的类SpringApplication.run("classpath:/applicationContext.xml", args);

回答by Jason Im

I recommend Thymeleaf(template engine), jRebel for personal developer. Thymeleaf template files are just HTML resources. So, they`re changed immediately after you edit template files.

我推荐Thymeleaf(模板引擎),jRebel 给个人开发者。Thymeleaf 模板文件只是 HTML 资源。因此,它们会在您编辑模板文件后立即更改。

回答by Fred K

You can get hot swapping:

您可以获得热插拔:

  • for java code: using spring-loaded
  • for Thymeleaf templates: disabling the cache
  • 对于java代码:使用spring-loaded
  • 对于 Thymeleaf 模板:禁用缓存

Check this post to see more details: http://blog.netgloo.com/2014/05/21/hot-swapping-in-spring-boot-with-eclipse-sts/

查看此帖子以查看更多详细信息:http: //blog.netgloo.com/2014/05/21/hot-swapping-in-spring-boot-with-eclipse-sts/

回答by David Groomes

In Intellij, I can get this behavior. When the program is running in debug mode, select Run > Reload Changed Classes

在 Intellij 中,我可以得到这种行为。当程序在调试模式下运行时,选择Run > Reload Changed Classes

Note: After Intellij completes the action, it might say Loaded classes are up to date. Nothing to reload. This is misleading, because it actually DID reload your classpath resources.

注意:Intellij 完成操作后,可能会显示Loaded classes are up to date. Nothing to reload. 这是一种误导,因为它实际上确实重新加载了您的类路径资源。

My environment/setup includes:
Intellij 13
Embedded Tomcat
Run/Debug configuration of type 'Application' (which just uses a main class)
Serving static html, css and js (no jsp)

我的环境/设置包括:
Intellij 13
Embedded Tomcat
Run/Debug configuration of type 'Application'(它只使用一个主类)
Serving static html, css and js (no jsp)

回答by jonashackt

From 1.3.0. (now in Milestone 2) on, you can use the spring-boot-devtools for that as a lightweigt approach - see the docsor this blogpost. Simply upgrade to >= 1.3.0. and add the following to your pom.xml:

从 1.3.0. (现在在里程碑 2)上,您可以使用 spring-boot-devtools 作为一种轻量级的方法 - 请参阅文档这篇博文。只需升级到 >= 1.3.0。并将以下内容添加到您的 pom.xml 中:

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

Than start your SpringBootApplication with Run As... and you′re fine.

比用 Run As... 启动你的 SpringBootApplication,你就没事了。

回答by Rafael Lima

If you're using maven, the spring-boot-maven-plugin in your pom.xml needs to be like this to get the hot swap:

如果您使用的是 maven,则 pom.xml 中的 spring-boot-maven-plugin 需要像这样才能获得热插拔:

  <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>springloaded</artifactId>
                <version>1.2.0.RELEASE</version>
            </dependency>
        </dependencies>
    </plugin>

and if you're using thymeleaf, add this to your application properties:

如果您使用的是百里香叶,请将其添加到您的应用程序属性中:

spring.thymeleaf.cache=false

But remember something: Don't use this in your production environment..

但请记住:不要在您的生产环境中使用它。

回答by Igor

I do not know how far this kind of support goes, but in case you use Eclipse IDE (or anyone reading this): starting up your Spring-Boot application via m2e in debug-mode(press the "Debug"-dropdown button and pick your maven run configuration item).

我不知道这种支持有多远,但如果您使用 Eclipse IDE(或任何阅读本文的人):在调试模式下通过 m2e 启动您的 Spring-Boot 应用程序(按“调试”下拉按钮并选择您的 Maven 运行配置项)。

It works for me like a charm.

它对我来说就像一种魅力。

My maven run configuration item is configured as follows:

我的maven运行配置项配置如下:

  • goal is set to "spring-boot:run"
  • base directory is the project directory
  • 目标设置为“spring-boot:run”
  • 基目录是项目目录

I am not usingany further libraries (not even spring-boot-devtools).

没有使用任何其他库(甚至没有使用spring-boot-devtools)。

That's it.

就是这样。

回答by shabby

but isn't there a built-in option of some sort when working with IntelliJ/Eclipse?

但是在使用 IntelliJ/Eclipse 时是否有某种内置选项?

What helped me in IntelliJ 15.0, windows 10, was the following sequence:

在 IntelliJ 15.0、Windows 10 中帮助我的是以下序列:

STEP 1:Added the following dependency in pom (This is mentioned everywhere but this alone dint solve it), as mentioned by @jonashackt

第 1在 pom 中添加以下依赖项(到处都提到了这一点,但仅此一项就解决了),如@jonashackt 所述

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

STEP 2:Then from File->Settings-> Build-Execution-Deployment -> Compiler (make sure main compiler option is selected and not any of its sub-options)

第 2然后从 File->Settings-> Build-Execution-Deployment -> Compiler(确保选择了主编译器选项而不是其任何子选项)

enable Make Project Automatically. Click ok and close the dialog

启用自动制作项目。单击确定并关闭对话框

STEP 3:Hold Shift+Ctrl+A(on windows) you will see a search dialog with title "Enter Action or option name", type registry. Double click the first option that says "Registry..." it will open another window. Look for the following option:

第 3按住Shift+ Ctrl+ A(在 Windows 上)您将看到一个标题为“输入操作或选项名称”的搜索对话框,键入注册表。双击显示“注册表...”的第一个选项,它将打开另一个窗口。寻找以下选项:

compiler.automake.allow.when.app.running

and enable it, click close

并启用它,单击关闭

STEP 4:Restart IDE

第 4 步:重新启动 IDE

Elaborated from this source

从这个来源详细说明