Learning Spring Boot: (5) Hot deployment using devtools

foreword

spring-boot-devtools is a module for developers, the most important function of which is to automatically apply code changes to the latest App. The principle is to restart the application after discovering that the code has changed, but the speed is faster than manually stopping and restarting. Faster does not mean the time saved by manual operation.

The deep principle is to use two ClassLoaders, one ClassLoader loads those classes that will not change (third-party Jar packages), and the other ClassLoader loads classes that will change, called restart ClassLoader

, In this way, when there is a code change, the original restart ClassLoader is discarded, and a restart ClassLoader is re-created. Since there are fewer classes to be loaded, a faster restart time (within 5 seconds) is achieved.

text

Step 1: Add dependencies

<!--  
           devtools可以实现页面热部署(即页面修改后会立即生效,这个可以直接在application.properties文件中配置spring.thymeleaf.cache=false来实现),      
           实现类文件热部署(类文件修改后不会立即生效),实现对属性文件的热部署。   
           即devtools会监听classpath下的文件变动,并且会立即重启应用(发生在保存时机),注意:因为其采用的虚拟机机制,该项重启是很快的      
        -->  
       <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-devtools</artifactId>  
            <optional>true</optional>  
        </dependency>  

Step 2: Configure spring-boot-maven-plugin

<build>  
       <plugins>  
           <!--  
             用于将应用打成可直接运行的jar(该jar就是用于生产环境中的jar) 值得注意的是,如果没有引用spring-boot-starter-parent做parent,  
                       且采用了上述的第二种方式,这里也要做出相应的改动  
             -->  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
                <configuration>  
                   <!--fork :  如果没有该项配置,肯呢个devtools不会起作用,即应用不会restart -->  
                    <fork>true</fork>  
                </configuration>  
            </plugin>  
       </plugins>  
   </build>  

Step 3: Turn on the automatic compilation of the compiler

  • Eclipse Project Check whether Build Automatically is turned on in the run options. If it is not checked, check it. Eclipse is automatically compiled by default.
  • IDEA will automatically compile when it is not RUN or DEBUG by default. Therefore, after we start Spring Boot, when we modify the class again, it will not be automatically compiled. The process of enabling automatic compilation in the Run state is as follows:
    1. setting -> build
      springboot-5-1.png
    2. Shift+Ctrl+Alt+/, select Registry;
    3. Find the following option and check it:
      springboot-5-2.png
      mine is already checked, so it is a blue color prompt, just look for the one starting with c for the first time.

Step 4: Run the Test

Start the project and find devtools in the run dashboard:
springboot-5-3.png
1. Modify the class file and restart the project;
2. Modify the configuration file and restart the project.

Replenish

  1. If set SpringApplication.setRegisterShutdownHook(false), automatic restart will have no effect.
  2. By default, /META-INF/maven, /META-INF/resources, /resources, /static, /templates, /publicmodification of files in these folders will not cause the application to restart, but will be reloaded (devtools has a LiveReload server embedded, which refreshes the browser when the resource changes).
  3. If you want to change the default settings, you can set the directory that does not restart: spring.devtools.restart.exclude=static/**,public/**, in this case, only the file modification in these two directories will not cause the restart operation.
  4. If you want to add other exclusion directories on the basis of keeping the default settings: spring.devtools.restart.additional-exclude;
  5. If you want to make the application restart when files not on the classpath change, use: spring.devtools.restart.additional-paths, so that devtools will list the directory in the scope of monitoring.
  6. Set the spring.devtools.restart.enabled property to false to disable 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);
}

Reference article:

Guess you like

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