[Spring Boot] Development environment hot deployment

Development Environment Hot Deployment

In the actual project development and debugging process, the background class files will be frequently modified, resulting in the need to recompile and restart. The whole process is very troublesome and affects development efficiency. Here's how Spring Boot solves this problem.

1. The implementation principle of devtools

When we develop and debug Spring Boot projects, we need to go through the process of recompiling and restarting the program. Since a series of components and dependent packages need to be loaded when the system starts, the whole process is very time-consuming and greatly affects development efficiency.

Spring Boot has done a good job in this regard, providing the spring-boot-devtools component, so that the project can be recompiled and started without manually restarting the Spring Boot application, which greatly shortens the compilation and startup time, thereby improving development efficiency.

The core of spring-boot-devtools is two class loaders (ClassLoader): one is the Base class loader (Base ClassLoader) , which is responsible for loading classes that will not change, such as third-party JAR packages, etc.; the other is Restart class loading Device (Restart ClassLoader) , responsible for loading those classes that will change under development. In this way, if you only modify the Java code, devtools will only reload the modified class file, and will not reload other third-party JAR packages, so the restart is faster, thereby achieving the effect of hot deployment.

After we introduce the devtools component into the project, devtools will monitor the file changes under the classpath, recompile and regenerate the class files when the files are modified; devtools will monitor the changes of the class files, trigger the Restart class loader to reload the class, and thus Realize hot deployment of class files and property files.

It should be noted that not all changes need to restart the application (such as static resources, view templates). We can specify some file or directory modifications without restarting the application by setting the spring.devtools.restart.exclude property. For example, all file updates under /static and /public can be set not to trigger application restart.

2. Configure development environment hot deployment

Step 01 Add dev-tools dependency in the pom.xml configuration file.

insert image description here
In the above example, use optional=true to indicate that the dependency will not be passed, that is, the project depends on devtools; if other projects import the JAR package generated by this project, devtools will not be included, and if you want to use devtools, you need to re-introduce.

Step 02 Configure devtools in application.properties.

insert image description here
The above configuration is mainly to enable devtools hot deployment, then specify the background file directory for monitoring, and finally specify the directory where file changes do not need to be recompiled and deployed. After the configuration is complete, devtools will monitor the file changes in the classpath and restart the application immediately.

It should be noted that devtools can also implement hot deployment of the foreground page, that is, it will take effect immediately after the page is modified. You need to configure spring.thymeleaf.cache=false in the application.properties file to specify not to cache the front-end page.

Step 03 Verify whether the configuration takes effect.

After the configuration is complete, you need to verify whether hot deployment takes effect. First start the project, modify a java file in the project, and then you can see that devtools automatically restarts the application in the IDEA background.

Modify the java background code and you will find that Spring Boot has recompiled the file, and then automatically reloads it without manual restart.

Guess you like

Origin blog.csdn.net/weixin_45627039/article/details/131658801