Spring Boot After the developer tool spring-boot-devTools is installed, you can listen to file changes in the classpath and automatically restart the service. This function greatly improves the efficiency of service development.

Spring-boot-devtools uses two classloaders, one for loading dependent classes from third parties and the other for loading classes built by project source code. Restarting the service will only destroy and rebuild the latter, so the restart is faster.

However, frequent file modification may lead to frequent service restart, which may lead to IDE performance problems and lag phenomenon. The solution is to change the service restart time from automatic trigger to active trigger after the file is modified. The Spring Boot developer tool provides a mechanism for actively triggering restarts by listening for changes to a specific file (see here), which is specified by the following configuration.

spring.devtools.restart.trigger-file=.reloadtrigger
Copy the code

The.reloadTrigger above should be placed in the classpath search path, for example in the Resources directory.

src
+- main
   +- resources
      +- .reloadtrigger
Copy the code

After the configuration, when you need to trigger a restart, you can simply modify the.reloadTrigger file. For example, on Linux, you can modify the file in this way (manually or by other means, as long as the contents of the file change).

echo $(date) > ./src/main/resources/.realoadtrigger
Copy the code

The above code is placed in a script that triggers the Spring Boot service to restart each time the following is executed.

Tools such as IDEA provide restart operations that take advantage of the above principles. A similar one-click reboot can be implemented in VSCode. The method is to configure a Build Task (see figure below) and then press Shift+Command+B in VSCode to trigger the Spring Boot service to restart.

The Build Task allocation

{
    "version": "2.0.0"."tasks": [{"label": "Restart service"."type": "shell"."command": "echo $(date) > ${workspaceFolder}/src/main/resources/.reloadtrigger"."problemMatcher": []."presentation": {
                // Execute silently, otherwise the output panel will pop up with each reboot
                "reveal": "silent"
            },
            "group": {
                "kind": "build"."isDefault": true}}}]Copy the code

The end!