In the actual development process, every time you modify the code, you have to restart the project and re deploy it. For some large applications, the restart time costs a lot of time. For a back-end developer, the restart process is really hard. In the field of java development, hot deployment has always been a difficult problem to solve. The current Java virtual machine can only realize the hot deployment of method body modification. For the structure modification of the whole class, it still needs to restart the virtual machine and reload the class to complete the update operation. Let's see how to implement hot deployment for simple class modification.
The deep principle is that two classloaders are used. One loads the classes that will not change (third-party jar package), and the other loads the classes that will change, which are called restart classloader. In this way, when the code changes, the original restart classloader will be discarded and a restart classloader will be created. Because there are fewer classes to load, So it achieves faster restart time.
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- Devtools hot deployment -- > <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> <scope>true</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> </plugins> </build>
Configure devtools in application. YML
spring: devtools: restart: Enabled: true # set to enable hot deployment Additional paths: Src / main / Java # restart directory exclude: WEB-INF/** freemarker: Cache: false # the page does not load the cache, and the modification takes effect immediately
When we modify the class file, idea will not compile automatically. We have to modify the idea settings.
- （1）File-Settings-Compiler-Build Project automatically
- (2) CTRL + Shift + Alt + /, select registry, and check compiler automake allow when app running
This work adoptsCC agreementReprint must indicate the author and the link of this article