Learn Spring Boot: hot deployment using devtools

Keywords: Database MySQL Spring Boot


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.


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      

Step 2: configure spring boot Maven plugin

             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  
                   <!--fork :  What if there is no such configuration devtools Will not work, i.e. the application will not restart -->  

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:
    1. setting -> build

    2. Shift+Ctrl+Alt + /, select Registry;

    3. 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:

  1. Modify class files and restart the project;
  2. Modify the configuration file and restart the project.


  1. If springapplication.setregistershutdownhook is set (false), automatic restart will not work.
  2. 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).
  3. 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.
  4. If you want to keep the default settings, you need to add other exclusion directories: spring. Devtools. Restart. Additional exclude;
  5. 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.
  6. 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){
    SpringApplication.run(MyApp.class, args);

Posted by shawngoldw on Tue, 16 Nov 2021 08:35:17 -0800