Introduction to Spring DevTools
Spring Boot includes an additional set of tools that can make the application development experience more enjoyable.spring-boot-devtools
Modules can be included in any project, and it can save a lot of time. To use devtools support, just add the module dependencies to your build:
Maven.
<dependencies> <dependency> <groupId></groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency> </dependencies>
Gradle.
dependencies { compile(":spring-boot-devtools") }
Developer tools are automatically disabled when running packaged applications. If you passjava -jar
Or when other special class loaders are started, they will be considered "applications of production environments".
Mark dependencies asoptional
Optional is a best practice to prevent devtools dependencies from being passed into other modules. Gradle does not support optional dependencies out of the box, you can refer topropdeps-plugin
。
1. Default value of attributes
Some libraries supported by Spring Boot use caches to improve performance. For example, the template engine will cache the compiled templates to avoid repeated parsing of template files. Additionally, Spring MVC can add HTTP cache headers to the response when serving static resources.
While cache is very beneficial in production, it can have a counterproductive effect during development and it will prevent you from seeing the changes you just made in the application. Therefore, spring-boot-devtools will disable these cache options by default.
The cache option is usually inConfiguration in the file. For example, Thymeleaf provides
property.
spring-boot-devtools
The module does not need to manually set these properties, but automatically applies reasonable development-time configuration.
2. Automatic restart
spring-boot-devtools
It will automatically restart when a file on the classpath changes. This can be a useful feature when working in the IDE, as it provides a very fast feedback loop for code changes. All changes on the classpath are monitored by default, but be aware that some resources (such as static resources and view templates) do not require the application to be restarted.
Trigger restart
When DevTools monitors a classpath resource, the only way to trigger a restart is to update the classpath. The way the classpath is updated depends on the IDE you are using. In Eclipse, saving the modified file will cause the classpath to be updated and a restart will be triggered. In IntelliJ IDEA, the build project (Build -> Make Project) will have the same effect.
Restart and reload
The restart technology provided by Spring Boot uses two class loaders. Classes that are not changed (for example, from third-party jars) are loaded into the base class loader. The class you are developing is loaded into the restart class loader. When the application restarts, the restart loader will be discarded and a new class loader is created. This approach means that application restarts are usually much faster than "cold boot" because the base loader is already loaded and available.
1. Exclude resources
Some resources do not necessarily need to trigger a restart when they are changed. For example, you can edit the Thymeleaf template directly. By default, changing resources in /META-INF/maven , /META-INF/resources , /resources , /static , /public or /templates does not trigger a restart, but triggers a live reload. If you want to customize these exclusions, you can use properties. For example, to exclude only /static and /public you will set the following:
= static / **,public / **
If you want to keep the default (in case) value above and add other exclusions, you can use-exclude
property.
2. Monitor additional paths
When you make changes to files that are not in the classpath, you may need to restart or reload the application. To do this, use the -paths property to configure monitoring changes to other paths. You can use the above properties to control whether changes under the attached path will trigger a full restart or just live reload.
3. Disable restart
If you don't want to use the restart function, you can use itattribute to disable it. In most cases, you can
Set this item in (this will still initialize the restart class loader, but will not monitor file changes).
For example, if you need to disable restart support completely because it does not work for a specific library, you need to call it(…)
Previous settingsSystem
property. For example:
public static void main(String[] args) { ("", "false"); (, args); }
4. Use trigger file
If you use an IDE that automatically compiles the changed files, you may want to trigger the restart only at a specific time. To do this, you can use "trigger file", which is a special file that you have to modify when you want to actually trigger the restart check. Changing the file only triggers a check and will only restart if Devtools detects that it must perform some actions. The trigger file can be updated manually or through the IDE plug-in.
To use a trigger file, use-file
property.
If you want to set -file to global configuration, you can refer to the fourth section below.
5. Custom restart class loader
As described in the Restart and Reload section above, the restart function is implemented by using two class loaders. For most applications, this method works well, but can sometimes cause class loading issues.
By default, any open project in the IDE will be loaded using the "restart" class loader, any regular.jar
The file will be loaded using the "base" class loader. If you work on a multi-module project, but not every module is imported into the IDE, you may need to customize the configuration. To do this, you can create aMETA-INF/
document.
Files can contain
.
and.
The attribute of the prefix.include
Elements are items that should be placed in the "restart" class loader.exclude
Elements are items that should be placed in the "base" class loader. The value of the property is a regular expression applied to the classpath.
For example:
=/mycorp-common-[\\w-]+\.jar =/mycorp-myproj-[\\w-]+\.jar
For general mappers, the following configuration can be done:
=/mapper-[\\w-\\.]+jar
All attribute key values (name, companycommonlibs part) must be unique, only.
and.
The attributes at the beginning are valid.
All classpaths belowMETA-INF/
The configuration files will take effect, so you can package the configuration into each module.
Note: The new version of Mapper (3.4.1+) will add this configuration by default.
6. Known Limitations
Restart function for use standardsObjectInputStream
Object serialization objects are not very good. If you need to deserialize the data, you may need to use Spring'sConfigurableObjectInputStream
Cooperate().getContextClassLoader()
use.
Unfortunately, some third-party libraries do not consider deserialization with context class loaders. If you find such a problem, you need to request a fix from the original author.
3. Real-time loading
spring-boot-devtools
The module contains an embedded LiveReload server that can be used to trigger a browser refresh when the resource changes. The LiveReload browser extension supports Chrome, Firefox and Safari, which you can download from for free.
If you don't want to start the LiveReload server while the application is running, you canThe property is set to
false
。
Only one LiveReload server can be run at the same time. Before starting the application, make sure no other LiveReload server is running. If you start multiple applications from the IDE, only the first application will support LiveReload.
4. Global settings
You can$HOME
Add the folder name as.
to configure global devtools settings (note that the file name begins with "."). Any properties added to this file will apply to all Spring Boot applications that use devtools on your computer. For example, to configure restart to always use trigger files, you can add the following:
/.。
-file=.reloadtrigger
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for your study or work. Thank you for your support. If you want to know more about it, please see the following links