What is hot deployment
Everyone knows that during project development, the page data or the data structure is often changed. In order to display the change effect, you often need to restart the application to view the change effect. In fact, it is a new Class file that is recompiled and generated. This file records various information corresponding to the code, and then the Class file will be loaded by the ClassLoader of the virtual machine.
Hot deployment takes advantage of this feature. It listens to the fact that if the Class file is changed, it will create a new ClassLoader to load the file. After a series of processes, the results will finally be presented to us.
Class loading mechanism
The compiler can compile the code into a Class file that stores bytecode. The Class file stores various information and will eventually be loaded into a virtual machine for running and use. Class loading mechanism (excerpted from "In-depth Understanding of Java Virtual Machine") The virtual machine loads the data describing the class from the Class file into memory, and checks, converts, parses and initializes the data, and finally forms a Java type that can be used directly by the virtual machine.
Spring Boot implements hot deployment
Spring Boot implements hot deployment in the following ways:
Spring Loaded
This method is loaded in the form of a Maven plug-in, so it is started using the Maven command mvn spring-boot:run when starting, but it is started using Application.run. It is invalid because the Maven plug-in mechanism has been bypassed when starting the application.
Pom integration method:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> <version>1.2.5.RELEASE</version> </dependency> </plugin> </plugin> </plugins></build>
spring-boot-devtools
This method can be used to restart the application after modifying the file.
pom integration:
<!-- Hot Deployment Module--><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <!-- This requires true hot deployment to be effective--></dependency>
Integration attention
#Shield thymeleaf cache (suggestion: the development environment is set to false, the generation environment is set to true) spring.thymeleaf.cache=false
1. For devtools, you can specify directories or exclude directories for hot deployment.
#The file that adds that directory requires restartspring.devtools.restart.additional-paths=src/main/java#The file that excludes that directory does not require restartspring.devtools.restart.exclude=static/**,public/**
Set idea to enable file modification to automatically restart the project
1. Find the Preferences of idea -> Build, Execution, Deployment -> Compiler, check Build project automatically
2. Return to the normal interface of idea. Use the shortcut key shift+option+command+/ for Mac. The shortcut key on window is Shift+Ctrl+Alt+/. Open Registry and check it.
compiler.automake.allow.when.app.runningcompiler.automake.allow.when.app.running
Through the above settings, you can load html without restarting the service, but if you modify the java file, the service will automatically restart in a few seconds. If you do not want the service to restart, you need to add spring.devtools.reatart.enable=false in application.properties or application.yml.
Summarize
The above is the hot deployment of Spring Boot integrated spring-boot-devtools that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!