introduction
During Java development, developers often need to frequently modify the code and view the effects. At this time, the traditional restart of the application will take a lot of time and seriously affect the development efficiency.
Spring Boot DevTools came into being, providing a powerful set of development tools, with hot deployment capabilities particularly striking.
1. Overview of Spring Boot DevTools
Spring Boot DevTools is a development tool module provided by Spring Boot, designed specifically to improve developer efficiency. It includes practical functions such as automatic restart, automatic browser refresh, and remote debugging. DevTools disables template caching by default, enables LiveReload servers, and configures a reasonable logging level, allowing developers to focus more on the code itself rather than on the environment configuration.
Introducing DevTools in Maven project is very simple, just add the following dependencies:
<dependency> <groupId></groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> <!-- Mark dependencies as optional,Avoid passing to other modules --> </dependency>
For Gradle projects, you can configure it like this:
developmentOnly ':spring-boot-devtools'
It is worth noting that when the application is packaged in production, DevTools is automatically disabled, so there will be no impact on the production environment.
2. Automatic restart mechanism
2.1 Working principle
Spring Boot DevTools' automatic restart function is one of its most core features. It enables a quick restart by using two class loaders: one loads a class that does not change (such as a third-party jar package), and the other loads a class that changes frequently (such as business code in the project).
When a file in the classpath is detected to change, DevTools will only reload the changed class, rather than the entire application, greatly shortening the restart time.
Here is a simple Spring Boot application example that shows the basic usage of DevTools automatic restart:
package ; import ; import ; import ; import ; @SpringBootApplication public class DevToolsDemoApplication { public static void main(String[] args) { (, args); } } @RestController class HelloController { @GetMapping("/hello") public String hello() { // Modify the return value of this method and save it, and the application will automatically restart return "Hello, DevTools!"; } }
2.2 Custom restart trigger
DevTools monitors changes in classpath resources by default. However, in some scenarios, we may need to customize the file that triggers the restart. By configurationand
-paths
Properties that can accurately control which files changes will trigger restarts.
# # Exclude specific paths, modifying files under these paths will not trigger restart=static/**,public/** # Add additional monitoring paths, file changes under these paths will trigger restart-paths=src/main/resources/templates
If you need to completely disable the automatic restart function, you can set:
=false
It can also be controlled at startup through system properties:
public static void main(String[] args) { // Disable restart function through system properties ("", "false"); (, args); }
3. LiveReload support
3.1 Automatic browser refresh
DevTools has a built-in LiveReload server, which can automatically trigger browser refresh when application resources change. This function works in conjunction with automatic restart, allowing developers to see the effect immediately after modifying the code without manually refreshing the browser.
To use the LiveReload function, you need to install the corresponding extension plug-in in the browser. Mainstream browsers such as Chrome, Firefox, etc. all provide LiveReload plug-ins. After the installation is complete, enable the plug-in when accessing the application, and DevTools' LiveReload server will establish a connection with the browser.
By default, the LiveReload server listens to port 35729. If you need to change the port or disable this feature, you can configure it through the following:
# Disable LiveReload function=false # Modify the LiveReload server port=35730
3.2 Integration with front-end framework
When Spring Boot applications are combined with modern front-end frameworks (such as React, Vue, etc.), a more powerful development experience can be achieved. Here is an example of integration:
// Backend Controller example@RestController @RequestMapping("/api") public class ApiController { @GetMapping("/data") public Map<String, Object> getData() { // Modify the return data here, the application will restart and trigger the front-end refresh Map<String, Object> data = new HashMap<>(); ("message", "Data updated"); ("timestamp", ()); return data; } }
Front-end code:
// Front-end code example (assuming it is used)new Vue({ el: '#app', data: { message: '', timestamp: 0 }, created() { (); }, methods: { fetchData() { fetch('/api/data') .then(response => ()) .then(data => { = ; = ; }); } } });
4. Adjust the default value of attributes
4.1 Cache Configuration
DevTools will automatically disable multiple caching options to ensure that changes can be seen in real time during development. This includes template engine cache, static resource cache, etc. For Thymeleaf, FreeMarker, Groovy templates, etc., DevTools will disable its caching function.
// Thymeleaf template example@Controller public class ViewController { @GetMapping("/view") public String view(Model model) { // After modifying the model data, the page will immediately reflect changes without restarting ("currentTime", new Date()); return "example"; } }
Corresponding Thymeleaf template:
<!DOCTYPE html> <html xmlns:th=""> <head> <title>DevTools Demo</title> </head> <body> <h1>Current time</h1> <p th:text="${currentTime}">The time will be displayed here</p> </body> </html>
4.2 Log configuration
DevTools sets reasonable log levels for common web development libraries to help developers obtain appropriate log output during development. Can be customizedTo override these default settings:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <!-- IntroducedSpring BootDefault configuration --> <include resource="org/springframework/boot/logging/logback/" /> <!-- Custom log level --> <logger name="" level="DEBUG"/> <logger name="" level="TRACE"/> <!-- Other configurations --> </configuration>
5. Remote development support
5.1 Configuring Remote Applications
DevTools not only supports local development, but also provides remote development capabilities. Remote debugging allows applications to be updated in real time and monitored in a remote environment. To enable remote support, applications and development tools are required.
First, add DevTools dependencies in the remote application:
<dependency> <groupId></groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional> </dependency>
And enable remote support in the application properties:
# Enable remote development support, and the production environment must be disabled!=mysecret
5.2 Using a remote client
In a local development environment, you can use the DevTools client to connect to a remote application. Usually start the client via Maven or Gradle plugin:
# Use Maven to start the remote clientmvn spring-boot:run -="-=mysecret -="
The following is an example of a remotely developed Java configuration class:
package ; import ; import ; @Configuration @Profile("dev") // Only effective in the development environmentpublic class DevToolsRemoteConfig { // Remote development related configuration // When this type changes, the remote application will receive updates and restart // Note: Remote development functions should be used with caution and appropriate security measures should be taken // If you use strong passwords, restrict access to IP, etc.}
Summarize
Spring Boot DevTools provides Java developers with a powerful set of tools that significantly improve development efficiency. Through the automatic restart mechanism, developers can view the code change effect without manually restarting the application; with the help of LiveReload function, the browser can automatically refresh and display the latest page in real time; the default cache disability and log configuration provide the best experience for the development environment; remote development support extends the application scenarios of DevTools.
The rational use of DevTools can build an efficient and smooth development workflow, reduce waiting time, and enable developers to focus more on the code logic itself.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.