Spring Boot implements hot deployment during development

Hot deployment principle:

It monitors that if a Class file is changed, it will create a new ClaassLoader to load the file, and after a series of processes, the result will finally be presented to us.

Class loading mechanism:

Classes in Java can be compiled into Class files that store bytecodes by the compiler. The Class files store various information and are ultimately loaded into the virtual machine for running. The virtual machine loads the data describing the class from the Class file into the memory, and verifies, converts, parses and initializes the data, and finally forms a Java type that can be directly used by the virtual machine.

Spring Boot implements hot deployment during development:

Spring Boot implements hot deployment in the following ways


Using spring-boot-devtools with Spring Loaded

Spring Loaded
is loaded in the form of a Maven plug-in, so use the Maven command mvn spring-boot:run to start at startup, and start through Application.run will be invalid, because it has been bypassed when starting through the application. Maven plugin mechanism.
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>
</dependencies>
</plugin>
</plugins>
</build>

No matter how the application is started with spring-boot-devtools
, you can 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 It needs to be true for hot deployment to be effective -->
</dependency>
Integration Note
If you find that there is no hot deployment effect, you need to check whether automatic compilation is turned on in the IDE configuration.
If you use the Thymeleaf template engine, you need to set the template default cache to false

#Disable thymeleaf caching (recommendation: the development environment is set to false, the generation environment is set to true)
spring.thymeleaf.cache=false

1. For devtools, you can specify a directory or exclude a directory for hot deployment #Adding
files in that directory requires restart
spring.devtools.restart.additional-paths=src/main/java #Excluding
files in that directory does not require restart
spring.devtools .restart.exclude=static/**,public/**

Set the idea to let him implement file modification and automatically restart the project
1. Find the idea's Preferences -> Build, Execution, Deployment -> Compiler, check Build project automatically
2. Return to the normal interface of the idea, use the shortcut key shift+option+command+/ on Mac , the shortcut key on the window is Shift+Ctrl+Alt+/, open the Registry, check
compiler.automake.allow.when.app.runningcompiler.automake.allow.when.app.running
Through the above settings, you can restart the service without restarting In the case of loading html, but if you modify the java file, the service will automatically restart after 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

 

Reprinted in: https://www.cnblogs.com/yxfcnbg/p/11547433.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324756795&siteId=291194637