SoFunction
Updated on 2025-03-08

Detailed explanation of SpringBoot implements hot deployment

Here are a few of the main reasons for using Spring Boot hot deployment:

reason:

1. Improve development efficiency

Hot deployment enables developers to verify and test their code changes faster. No need to restart the application manually, just save the file after each modification to view the results immediately. This greatly shortens the development and debugging cycle and improves development efficiency.

2. Real-time debugging

With hot deployment, developers can dynamically debug code while the application is running. They can add breakpoints, check the value of variables, and observe the behavior of the code during the application run. This is very helpful for quickly positioning and solving problems.

3. Coding experience

Using hot deployment allows developers to keep them in a persistent encoding state without interruptions to manually restart the application. They can view the effects of their code modifications in real time, making the encoding process more smooth and coherent.

4. Reduce repeated operations

Hot deployment avoids frequent application restarts and reduces meaningless waiting time. Developers only need to save the file, and the system will automatically reload the relevant classes and resources, so that each code modification will take effect immediately. This helps reduce duplicate operations in the workflow and improves productivity.

principle

The principle of Spring Boot implements hot deployment mainly utilizes the class loading mechanism and file monitoring mechanism of Java virtual machine (JVM). Here are the basic principles of hot deployment:

1. Class loading mechanism

Java virtual machines use a class loader (ClassLoader) to load and link classes. When the application is running, the class loader will dynamically load the class and create the class object as needed. Spring Boot utilizes the Java virtual machine's class loading mechanism to achieve hot deployment by reloading the modified classes.

2. File monitoring mechanism

Spring Boot associates applications with file systems in the development environment and listens for file changes in associated folders. When a file change is detected, Spring Boot reloads the classes associated with changing the file.

The hot deployment of the project is achieved through the following methods:

-boot-devtools

This is the hot deployment tool provided by SpringBoot, adding dependencies:

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <optional>true</optional> 
</dependency>

Implement automatic restart after resource modification and other functions. When starting the application, DevTools automatically configures hot deployment and restarts the application when saving the file. DevTools also provides other features, such as automatic restart, automatic page refresh, etc., to improve development efficiency.

2. Use Spring Loaded

Spring LoadedSpring's hot deployment program implements automatic reload after modifying the class. The implementation principle is to use a custom ClassLoader, which can implement hot code replacement. The specific implementation is as follows:

2.1 Add Spring Loaded dependencies to the file:

<dependency>
    <groupId></groupId>
    <artifactId>springloaded</artifactId>
    <version>1.2.</version>
</dependency>

2.2 Configure the automatic build function of the project in the IDE or compiler. Make sure the project is automatically rebuilt when saving the file

2.3 When starting the application, add the following JVM parameters:

-javaagent:/path/to/ -noverify

in/path/to/It is the path of the Spring Loaded JAR file, and it is modified accordingly accordingly according to your actual situation.

2.4 Start the application and develop it

Whenever a file is saved, Spring Loaded automatically detects the changes and reloads the modified class so that your changes can take effect immediately.

It should be noted that Spring Loaded is a third-party library and there may be some limitations and instability when using it. Spring official no longer maintains Spring Loaded

Plugin

JRebel's paid hot deployment software requires adding JRebel plug-in to implement hot deployment of code. The effect is very good, but it needs to be paid for use.

Boot Maven plugin

You can monitor code changes and automatically restart the application.

<plugin>
    <groupId></groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <fork>true</fork>
    </configuration>
</plugin>

5. Set up hot deployment of Spring Boot project in IntelliJ IDEA

5.1 Open your Spring Boot project in IntelliJ IDEA.

5.2 Make sure that the Spring Boot DevTools plugin is installed. Can be passedFile -> Settings -> PluginsGo to the plugin management page, search and install the Spring Boot DevTools plugin.

5.3 In the top menu bar of IntelliJ IDEA, selectRun -> Edit Configurations

5.4 Pop-upRun/Debug ConfigurationsIn the dialog box, select the leftSpring Boot

5.5 on the rightSpring BootIn the configuration window,On-frame deactivationandOn-update actionOptions are set toUpdate classes and resources

  • On-frame deactivation: The updated policy configured when you switch to another window.
  • On-update action: The configured update policy when a file change is detected.

After this setting, when you switch to another window, the application will restart in the background, and when a file change is detected, the application will update the relevant classes and resources.

5.6 ClickApplyorOKButton saves configuration.

5.7 Click on the top menu bar of IntelliJ IDEABuild -> Build ProjectTo build your project.

5.8 After the build is complete, click the green arrow icon on the toolbar or use the shortcut keysShift + F10To run your Spring Boot application.

Now, when you modify the code and save the file, IntelliJ IDEA automatically reloads the changed classes and resources into the running application for hot deployment.

Note that hot deployment is only available for development environments and for some modifications, the application may be restarted for effect. Therefore, hot deployment is not recommended in production environments.

The above is the detailed explanation of SpringBoot's hot deployment. For more information about SpringBoot's hot deployment, please pay attention to my other related articles!