preface
Spring boot devtools is a module for developers. The most important function is to automatically apply code changes to the latest App. The principle is to restart the application after finding that the code has changed, but the speed is faster than restarting after manual stop. Faster does not refer to the saved manual operation time.
The deep principle is that two classloaders are used. One classloader loads classes that will not change (third-party Jar package), and the other classloader loads classes that will change, which is called restart ClassLoader
In this way, when there are code changes, the original restart ClassLoader is discarded and a restart ClassLoader is re created. Since there are few classes to load, a faster restart time (within 5 seconds) is realized.
text
Step 1: add dependency
<!-- devtools Page hot deployment can be realized (that is, the page will take effect immediately after modification, which can be directly implemented in application.properties Configuration in file spring.thymeleaf.cache=false To achieve), Realize the hot deployment of class files (class files will not take effect immediately after modification), and realize the hot deployment of property files. Namely devtools Can monitor classpath The application will be restarted immediately (at the time of saving). Note: because of the virtual machine mechanism, the restart is very fast --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
Step 2: configure spring boot Maven plugin
<build> <plugins> <!-- It is used to make the application run directly jar(Should jar It is used in the production environment jar) It is worth noting that if there is no reference spring-boot-starter-parent do parent, The second method above is adopted, and corresponding changes should be made here --> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <!--fork : What if there is no such configuration devtools Will not work, i.e. the application will not restart --> <fork>true</fork> </configuration> </plugin> </plugins> </build>
Step 3: turn on the automatic compilation of the compiler
- Eclipse Project checks whether Build Automatically is enabled in the Run option. If it is not checked, check it. Eclipse is compiled automatically by default.
- IDEA is compiled automatically by default when it is not run or DEBUG. Therefore, after starting Spring Boot, we will not compile automatically when modifying the class again. The process of starting automatic compilation in run state is as follows:
-
setting -> build
-
Shift+Ctrl+Alt + /, select Registry;
-
Find the following options and check them:
Mine has been checked, so it's a blue prompt. Just find the one starting with c for the first time.
-
Step 4: run the test
Start the project and find devtools on the run dashboard:
- Modify class files and restart the project;
- Modify the configuration file and restart the project.
supplement
- If springapplication.setregistershutdownhook is set (false), automatic restart will not work.
- By default, / META-INF/maven, / META-INF/resources, / resources, / static, / templates, / public file modifications in these folders will not restart the application, but will be reloaded (devtools has a LiveReload server embedded in it. When resources change, the browser will refresh).
- If you want to change the default settings, you can set the directories that will not be restarted: spring.devtools.restart.exclude=static/**,public / * *. In this way, only the file modifications in these two directories will not cause the restart operation.
- If you want to keep the default settings, you need to add other exclusion directories: spring. Devtools. Restart. Additional exclude;
- If you want to restart the application when the file under the non classpath changes, use spring.devtools.restart.additional-paths. In this way, devtools will include the directory in the listening range.
- Set the spring.devtools.restart.enabled property to false to turn off automatic restart. It can be set in application.properties or by setting environment variables.
public static void main(String[] args){ System.setProperty("spring.devtools.restart.enabled","false"); SpringApplication.run(MyApp.class, args); }