Spring-boot-devtools brings a lot of convenience to development. This paper summarizes some of its uses.
To use devtools, you first need to add module dependencies. Maven and Gradle are configured as follows:
Maven:
Grad:
When running a fully packaged application, the development tool will be disabled. When running with java -jar or special class loader, it will be regarded as a production environment. expert
Some libraries in Spring Boot will improve their performance through caching. For example, the module engine caches the compiled template to avoid repeatedly parsing the template file; Spring MVC sets the HTTP cache header when responding to static resources.
Enabling caching is very useful in a development environment, but it is counterproductive in a production environment. So spring-boot-devtools will disable caching for us by default, not manually.
At the same time, the development tool will also set the level of the Web log to DEBUG, so that you can see more detailed request and response information. If you want to record all request details, including possibly sensitive information, you can enable spring. Http. log- request-details.
If you don't want to use the default configuration, you can set spring.devtools.add-properties to false.
When the file on the classpath is modified, the application using spring-boot-devtools will automatically restart. This will be a useful function in the development process, because you can quickly see the changes caused by the modification.
Because DevTools monitors classpath resources, the only way to trigger a restart is to update the classpath. In Eclipse, files will be compiled automatically when they are saved by default, which will trigger the update of classpath, while in IntelliJ, you need to build projects manually (build->; Build a project). Of course, you can also set up automatic compilation in IntelliJ, but I don't think it is necessary.
Devtools is restarted through two class loaders. For classes that will not be modified, such as third-party libraries, use the base class loader, and the classes edited by users use the restart class loader. When the application is restarted, the restarted class loader will be discarded and a new class loader will be created, which will speed up the restart. If you feel that the restart is not fast enough, you can consider using JRebel to realize hot update through overloaded classes.
By default, an incremental condition assessment report is output every time the application is restarted. This report shows changes that are automatically configured when an application is modified, such as adding or removing Bean and configuring properties.
Add the following configuration to close this log:
Some resources do not need to trigger a restart when they are changed. By default, when resources in /META-INF/maven, /META-INF/resources, /resources, /static, /public or /templates are modified, the restart will not be triggered (but live reload will be triggered, see below).
If you want to customize the directories that need to be excluded, you can configure them through spring. Devtools.restart.exclude attribute (commas separate multiple directories). If you want to keep the default configuration and exclude other directories, you can configure it through spring. Devtools. restart. additional-exclude property.
Additional paths can be configured to listen for changes through spring. dev tools . restart . additional-paths。
If you don't want to use the restart function, you can set it through the spring. Devtools.restart.enabled property. In most cases, it can be set directly in application.properties If you want to completely disable restart, you need to set the property of spring.devtools.restart.enabledsystem to false before calling SpringApplication.run(…):
If you use an IDE that constantly compiles change files, you may prefer to trigger a restart only at certain times. We can use the "trigger file", which is a special file and must be modified when we want to actually trigger the restart check. Changing the file will only trigger the check, and it will only restart when Devtools detects that something must be done. Trigger files can be updated manually or by using IDE plug-ins.
Set the spring.devtools.restart.trigger-file property to the trigger file path.
Spring-boot-devtools module contains an embedded LiveReload server, which can be used to trigger browser refresh when resources change. The browser needs to install the LiveReload plug-in.
If you don't want to enable the LiveReload server, please set the properties of spring. Devtools.livereload.enabled is false.
Only one LiveReload server can run at a time. If you start multiple applications from the IDE, only the first application has the LiveReload function.
Create a .spring-boot-dev tools.properties file in the $HOME directory, and add global devtools settings to it. Any properties added to this file will be applied to all Spring Boot applications that use devtools on this computer.