hotswap-classloader is a dynamic class loader based on the JVM. It utilizes the HotSwapWatcher and HotSwapClassloader technologies to dynamically detect modifications to class files. This project was inspired by the hot loading design of jfinal-undertow. See Reference
Key Features:
Loading Speed:
Comparable Products:
<dependency>
<groupId>com.litongjava</groupId>
<artifactId>hotswap-classloader</artifactId>
<version>1.2.0</version>
</dependency>
config.properties
file under src/main/resource/
and add the following content:mode=dev
SpringApplication.run(Application.class, args);
with SpringApplicationWrapper.run(Application.class, args);
.Example:
package com.litongjava.spring.boot.v216;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.litongjava.hotswap.wrapper.spring.boot.SpringApplicationWrapper;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplicationWrapper.run(Application.class, args);
}
}
Note: SpringApplicationWrapper
reads the mode
key value from the config.properties
file. If the value is dev
, it starts the hotswapwather to monitor class changes and enables hot reloading; otherwise, it does not activate.
Upon completing the above steps, you can refer to this project for integration:
View the integrated project
Calls ForkApp.run in its own startup
//params: startup class, startup parameters, hot load, restart class
ForkApp.run(SklearnWebApp.class, args, true, new SelfRestart());
For example
package com.litongjava.tio.boot.djl;
import org.tio.utils.jfinal.P;
import com.litongjava.hotswap.wrapper.forkapp.ForkApp;
public class SklearnWebApp {
public static void main(String[] args) throws Exception {
long start = System.currentTimeMillis();
// Initialize the server and start the server
P.use("app.properties");
// Diagnostic.setDebug(true);
// TioApplicationWrapper.run(SklearnWebApp.class, args);
ForkApp.run(SklearnWebApp.class, args, true, new SelfRestart());
long end = System.currentTimeMillis();
System.out.println("started:" + (end - start) + "(ms)");
}
}
Write SelfRestart to implement the methods in RestartServer
package com.litongjava.tio.boot.djl;
import com.litongjava.hotswap.debug.Diagnostic;
import com.litongjava.hotswap.kit.HotSwapUtils;
import com.litongjava.hotswap.server.RestartServer;
import com.litongjava.hotswap.wrapper.forkapp.ForkAppBootArgument;
import com.litongjava.tio.boot.TioApplication;
import com.litongjava.tio.boot.context.Context;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SelfRestart implements RestartServer {
public boolean isStarted() {
return ForkAppBootArgument.getContext().isRunning();
}
public void restart() {
System.err.println("loading");
long start = System.currentTimeMillis();
stop();
// get a new ClassLoader
ClassLoader hotSwapClassLoader = HotSwapUtils.newClassLoader();
if (Diagnostic.isDebug()) {
log.info("new classLoader:{}", hotSwapClassLoader);
}
// Set the context loader
Thread.currentThread().setContextClassLoader(hotSwapClassLoader);
// get startup class and args
Class<?> clazz = ForkAppBootArgument.getBootClazz();
String[] args = ForkAppBootArgument.getArgs();
// start
start(clazz, args);
long end = System.currentTimeMillis();
System.err.println("Loading complete in " + (end - start) + " ms (^_^)\n");
}
@Override
public void start(Class<?> primarySource, String[] args) {
Context context = TioApplication.run(primarySource, args);
ForkAppBootArgument.setContext(context);
}
@Override
public void stop() {
ForkAppBootArgument.getContext().close();
}
}
IDEA version is as follows:
HotSwapWatcher mainly listens to modifications of class files under target/classes
to trigger hot reloading. However, by default in IDEA, there is no automatic compilation, causing no changes to the files under target/classes
. There are two solutions:
Automatically Build Project
Search for "compiler" in settings, then check "build project automatically".
Allow Automatic Building Even When a Development Application is Running
Search for "make" in settings, then check "Allow auto-make to start even if developed application is currently running".
Adjust Delay Time
Use the Ctrl+Shift+Alt+/ shortcut, select "Registry...", then adjust the following configurations:
compiler.automake.postpone.when.idle.less.than
: Default is 3000, change to 100.compiler.automake.trigger.delay
: Default value is 3000, change to 100.compiler.document.save.trigger.delay
: Default is 1500, change to 100.After completing the above settings, modifying a file and saving it in IDEA will result in the file being automatically compiled, and the application will automatically restart with hot reloading applied.
Note: There might be an issue when a package contains only one .java
file. For more details, please check here.
If you aim to start the spring-boot project from the command line using mvn spring-boot:run
, the default class loader is plexus-classworlds
. To use this class loader, you need to follow these steps:
pom.xml
to enable plugin support for hot startup:<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
<fork>true</fork>
<mainClass>${start-class}</mainClass>
</configuration>
</plugin>
No setup required, natively supported. Modify a Java file and it will be loaded automatically after saving. Development experience is better than IDEA
No setup required, natively supported. Modify a Java file and it will be loaded automatically after saving. Development experience is better than IDEA
mvn spring-boot:run
After starting spring-boot, adding a method to the controller, and pressing Ctrl+S to save, the HotSwapClassloader detects file changes and automatically reloads the code. This process is completed in approximately 0.8 seconds.
After starting spring-boot, adding a method to the controller, and pressing Ctrl+S to save, the HotSwapClassloader detects the file changes and automatically reloads the code. However, in IDEA, due to a compilation delay of about 10 seconds, the entire reloading process takes approximately 10.8 seconds.
When starting the project from the command line using mvn spring-boot:run
, you can modify the code in eclipse or IDEA for testing. This test is based on a large project. A regular startup takes 9.5 seconds, while hot reloading takes 3.4 seconds.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。