preface

Otaku as a Java programmer, in daily work, often need to modify the code, and then restart the service, in the verification code is effective. If it is a small project, it is good that the restart speed is relatively fast and the waiting time is relatively short. However, as the project grows larger and is split into multiple services, some code changes may require multiple services to be restarted to take effect. Then you spend a lot of time waiting for the service to restart.

This will definitely not work, which will greatly affect my development efficiency. Is there a way to achieve this without restarting the project after modifying the code?

That affirmation is some, otherwise this article zha come of 😁.

Hot Swap

Since Java1.4, HotSwap has been introduced to the JVM, which updates class bytecodes at Debug time. So with hot deployment, you can load the modified code without restarting the service, but it can only be used to update the method body. IDEA, as a artifact, naturally supports this technology.

The configuration IDEA

Click on the currently running service, and then click Edit Configurations.

Click On the program you want to configure and select Update Classes and Resources On ‘Update’ Action and On Frame Deactivation. Click OK to implement hot deployment.

After the above configuration, after modifying the code. Just click on the hammer or recompile using the shortcut Command + F9 to make the changes take effect. It will also indicate how many classes have been re-read.

This is already hot deployment. However, Java virtual machines can only implement hot deployment of method body modification. If the structure of the entire class is modified, the VIRTUAL machine needs to be restarted and the class can be reloaded to complete the update operation.

test

The initial state

Method body modification

Class structure changes

Because hot deployment only supports changing the method body, class structure changes will generate an error and prompt you to restart.

DevTools

Although the simple hot deployment was realized by configuring IDEA in the previous section, it has obvious disadvantages that only the modified hot deployment of the method body can be realized. It’s obvious that it can’t meet your daily needs, so you need to use DevTools instead.

DevTools provides a module named spring-boot-DevTools for developers to support hot deployment of Spring Boot applications, improving developers’ development efficiency and eliminating the need to manually restart Spring Boot applications. It’s as simple as importing the following dependencies into your project.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-devtools</artifactId>
    <scope>runtime</scope>
</dependency>
Copy the code

Trigger restarting

DevTools is not technically a hot deployment, but a quick restart. Why do you say that? DevTools works by using two classloaders, a Base ClassLoader to load classes that won’t change (for example, jars from third parties) and a restart ClassLoader to load classes that are currently under development. So when the application restarts, the restart classloader is discarded and a new classloader is created. This means that application restarts are usually much faster than a “cold start” because the Base ClassLoader is already populated and available.

In a nutshell: By monitoring classpath resources, the application is automatically restarted when a file on the classpath changes, which is faster than a cold start because only the modified class needs to be re-read.

So, how do you update the classpath to trigger an automatic restart? It depends on the IDE you use:

  1. In Eclipse, saving the modified file causes the classpath to be updated and triggers a restart.
  2. In IntelliJ IDEA, you need to click the Build buttonCommand + F9Build projects to implement.

Configuring automatic Restart

IDEA does not have a function similar to Eclipse that automatically triggers a restart when a file is saved. That’s definitely there, just do the following two configuration steps to achieve it.

Note: you need to restore all the previous Settings.

1. Enable Build Project Automatically.

2. Use the keyboard shortcut: Ctrl + Alt + Shift + / bring up the Registry window, check the compiler. The automake. Allow. The when. App. Running options.

The new version is shown below:

conclusion

IDEA can only implement hot deployment of method body modification, which cannot meet daily usage requirements. Therefore, DevTools is recommended. But if you decide that rebooting isn’t fast enough for you. Consider using the JRebel plug-in.

At the end

If you feel helpful to you, you can comment more, more like oh, you can also go to my home page to have a look, maybe there is an article you like, you can also click a concern oh, thank you.

I am a different kind of tech nerd, making progress every day and experiencing a different life. See you next time!