简体   繁体   English

Spring Boot Devtools示例

[英]Spring Boot Devtools by Example

According to the Spring Boot Devtools docs , devtools won't run in "production mode"; 根据Spring Boot Devtools文档 ,devtools不会以“生产模式”运行; that is, if you execute your Spring Boot app with java -jar .., then it won't use devtools' built-in JVM magic. 也就是说,如果你使用java -jar ..,执行你的Spring Boot应用程序java -jar ..,那么它将不会使用devtools的内置JVM魔法。 However, the Spring Boot starter docs only show you one way of running your Spring Boot application...via java -jar... . 但是, Spring Boot入门文档只向您展示了运行Spring Boot应用程序的一种方法...来自java -jar...

So first I'm wondering: How do I run my Spring Boot app in non-production mode? 首先,我想知道: 如何在非生产模式下运行Spring Boot应用程序? I know you can run your app with Spring Boot CLI (eg spring run ), but is that the only way? 我知道你可以使用Spring Boot CLI运行你的应用程序(例如spring run ),但这是唯一的方法吗?

Also, the same devtools docs mention you can explicitly remove the devtools jar from your production binary by using some excludeDevtools option, but they never explain where/how to use this. 此外,相同的devtools文档提到您可以使用一些excludeDevtools选项从生产二进制文件中显式删除devtools jar,但它们从不解释在何处/如何使用它。 Any examples/ideas here? 这里有任何例子/想法吗?

1) One example of running a Spring Boot app in non-production mode (ie not from the JAR) is from an IDE (most specifically Eclipse or IntelliJ), where you can launch the application from the main method, or using the Spring Boot launch support. 1)在非生产模式(即不是来自JAR)中运行Spring Boot应用程序的一个示例来自IDE(更具体地说是Eclipse或IntelliJ),您可以从main方法或使用Spring Boot启动应用程序发起支持。 This is the most practical use of Dev Tools as it allows to continuously watch the changes in your application without restarting. 这是Dev Tools最实际的用途,因为它允许在不重新启动的情况下持续观察应用程序中的更改。

2) excludeDevTools is an option of the Spring Boot Maven Plugin http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/maven-plugin/repackage-mojo.html or the Gradle plugin - see http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-repackage-configuration . 2) excludeDevTools是Spring Boot Maven插件的一个选项http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/maven-plugin/repackage-mojo.html或Gradle插件 - 请参阅http:/ /docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-repackage-configuration

So in your case it would be bootRepackage { mainClass = 'demo.Application' excludeDevtools = true } 所以在你的情况下,它将是bootRepackage { mainClass = 'demo.Application' excludeDevtools = true }

To start with Spring boot and devtools made development easier. 从Spring启动开始,devtools使开发变得更容易。 Spring boot uses embedded servlet container ( tomcat,undertow,jetty) with this you can have production ready code even in non-production environment. Spring引导使用嵌入式servlet容器(tomcat,underow,jetty),即使在非生产环境中也可以使用生产就绪代码。

Coming to your question how to run Spring-boot application in non-production environments, we can run application directly from Spring tool suite IDE by right click -> run as Spring boot if it is local environment. 回到你的问题如何在非生产环境中运行Spring-boot应用程序,我们可以直接从Spring工具套件IDE运行应用程序,右键单击 - >如果是本地环境,则运行Spring引导。

If the build tool used is maven then from command prompt mvn boot:run , if it is gradle then gradle bootRun from root folder of project location in command prompt. 如果使用的构建工具是maven,那么从命令提示符mvn boot:run,如果它是gradle,则在命令提示符中从项目位置的根文件夹gradle bootRun。 If you are packing your application as uber jar then Java -jar .., also if your environment is Linux you can start as a process. 如果您将应用程序打包为超级jar,那么Java -jar ..,如果您的环境是Linux,则可以从一个进程开始。

Regarding ignoring devtools in production environment, Spring boot intelligently ignore devtools jar if we specify Spring-boot-devtools as optional scope in dependencies of your build tool(maven, gradle) . 关于忽略生产环境中的devtools,如果我们将Spring-boot-devtools指定为构建工具(maven,gradle)的依赖项中的可选范围,则Spring启动会智能地忽略devtools jar。 In gradle Spring boot gradle plugin takes care of ignoring devtools for final deployed jar or war 在gradle中,Spring引导gradle插件负责忽略最终部署的jar或war的devtools

Update :: To debug a Spring Boot application run with Gradle, as seen at http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-remote-debug-gradle-run596 更新::调试使用Gradle运行的Spring Boot应用程序,如http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#howto-remote-debug-gradle-run596所示

Add this to your build.gradle file: 将其添加到build.gradle文件中:

applicationDefaultJvmArgs = [ applicationDefaultJvmArgs = [

"-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=5005" ] “-agentlib:jdwp = transport = dt_socket,server = y,suspend = y,address = 5005”]

In IntelliJ Idea IDE or STS, setup a new Debug configuration and in the options select "Remote". 在IntelliJ Idea IDE或STS中,设置新的Debug配置,然后在选项中选择“Remote”。 The defaults should be fine, but pay attention to the port number and transport type ("socket"). 默认值应该没问题,但要注意端口号和传输类型(“socket”)。 Have fun! 玩得开心!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM