tio-boot整合hotswap-classloader實現(xiàn)熱加載方法實例
什么是 hotswap-classloader?
hotswap-classloader 是一款由開發(fā)者 litongjava 創(chuàng)建的 Java 動態(tài)類加載器。這個工具的核心功能是支持在 Java 應用運行時動態(tài)地更換或更新類定義,而無需重啟整個JVM。這種熱替換(hot swapping)的能力對于開發(fā)過程中的迭代和測試尤其有價值,因為它大大減少了等待應用重啟的時間。
什么是 tio-boot?
tio-boot
是一個基于 Java 的網(wǎng)絡編程框架,用于簡化網(wǎng)絡應用的開發(fā)。它提供了一套豐富的 API 和工具,使開發(fā)者能夠更容易地構建和部署網(wǎng)絡服務和應用。tio-boot
支持多種網(wǎng)絡協(xié)議,并且提供了高性能和可擴展性。
為什么將 hotswap-classloader 和 tio-boot 結合使用?
結合使用 hotswap-classloader
和 tio-boot
可以為 Java 網(wǎng)絡應用開發(fā)帶來以下幾個關鍵優(yōu)勢:
- 快速迭代和測試:通過使用
hotswap-classloader
,開發(fā)者可以在不重啟服務器的情況下實時更新類文件,從而實現(xiàn)快速迭代和即時測試。 - 提升開發(fā)效率:減少了重啟應用程序所需的時間,開發(fā)者可以更加專注于代碼的編寫和改進,從而提高工作效率。
- 適合敏捷開發(fā):在敏捷開發(fā)模式下,需要頻繁地進行更改和測試。
hotswap-classloader
的動態(tài)加載能力使得這一過程更加流暢和高效。
總的來說,結使用 hotswap-classloader
和 tio-boot
不僅提高了開發(fā)效率,而且增強了網(wǎng)絡應用開發(fā)的靈活性和便利性。這對于希望快速迭代和改進其網(wǎng)絡應用的開發(fā)團隊來說,是一個非常有價值的組合。
整合 hotswap-classloader,開啟熱加載有兩種方式,
- 在啟動中使用 TioApplicationWrapper 啟動
- 啟動配置類配置 hotswap-classloader
這里重點介紹第二種方式
如何在開發(fā)環(huán)境下使用 hotswap-classloader 和 tio-boot 實現(xiàn)動態(tài)類加載。
1. 添加 hotswap-classloader 依賴
首先,您需要在您的 Java 項目中添加 hotswap-classloader
依賴。在項目的 pom.xml
文件中添加以下依賴配置:
<properties> <hotswap-classloader.version>1.2.0</hotswap-classloader.version> <tio-boot.version>1.2.4</tio-boot.version> </properties> <dependencies> <dependency> <groupId>com.litongjava</groupId> <artifactId>tio-boot</artifactId> <version>${tio-boot.version}</version> </dependency> <dependency> <groupId>com.litongjava</groupId> <artifactId>hotswap-classloader</artifactId> <version>${hotswap-classloader.version}</version> </dependency> </dependencies>
這將確保您的項目能夠使用 hotswap-classloader
。
2. 創(chuàng)建啟動類
創(chuàng)建一個簡單的啟動類 HelloApp
來使用 tio-boot
啟動您的應用。這個類將定義一個基本的 HTTP 請求路徑和一個處理方法:
package com.litongjava.tio.web.hello; // 導入必要的類 @ComponentScan @Controller @RequestPath("/") public class HelloApp { public static void main(String[] args) { tio-boot.run(HelloApp.class, args); } @RequestPath() public String index() { return "index4"; } }
這個類中的 main
方法將啟動 tio-boot,而 index
方法將響應根路徑的 HTTP 請求。
3. 配置類加載器
創(chuàng)建 HotSwapClassLoaderConfig
類以配置動態(tài)類加載器。這個配置類在服務器啟動之前設置自定義的類加載器,以便于開發(fā)環(huán)境下的熱替換:
package com.litongjava.tio.web.hello.config; import com.litongjava.hotswap.kit.HotSwapUtils; import com.litongjava.jfinal.aop.Aop; import com.litongjava.jfinal.aop.annotation.BeforeStartConfiguration; import com.litongjava.jfinal.aop.annotation.Initialization; import com.litongjava.tio.boot.constatns.ConfigKeys; import com.litongjava.tio.boot.utils.Enviorment; import lombok.extern.slf4j.Slf4j; @BeforeStartConfiguration @Slf4j public class HowSwapClassLoaderConfig { @Initialization public void configClassLoader() { Enviorment enviorment = Aop.get(Enviorment.class); String env = enviorment.get(ConfigKeys.appEnv); if ("dev".equals(env)) { // 獲取自定義的classLoalder ClassLoader hotSwapClassLoader = HotSwapUtils.getClassLoader(); Thread.currentThread().setContextClassLoader(hotSwapClassLoader); log.info("hotSwapClassLoader:{}", hotSwapClassLoader); } } }
這里的配置邏輯將檢查應用環(huán)境,并在開發(fā)環(huán)境下設置自定義的類加載器。
4. 實現(xiàn)服務器監(jiān)聽器
創(chuàng)建 MyServerListener
類,該類實現(xiàn)了 TioBootServerListener
接口。在服務器啟動完成后,這個類將啟動 HotSwapWatcher
來監(jiān)聽類文件的變化:
package com.litongjava.tio.web.hello.config; import com.litongjava.hotswap.watcher.HotSwapWatcher; import com.litongjava.hotswap.wrapper.tio.boot.TioBootArgument; import com.litongjava.hotswap.wrapper.tio.boot.TioBootRestartServer; import com.litongjava.jfinal.aop.Aop; import com.litongjava.jfinal.aop.AopManager; import com.litongjava.tio.boot.constatns.ConfigKeys; import com.litongjava.tio.boot.context.Context; import com.litongjava.tio.boot.server.TioBootServerListener; import com.litongjava.tio.boot.utils.Enviorment; import lombok.extern.slf4j.Slf4j; @Slf4j public class MyServerListener implements TioBootServerListener { protected static volatile HotSwapWatcher hotSwapWatcher; @Override public void boforeStart(Class<?>[] primarySources, String[] args) { } @Override public void afterStarted(Class<?>[] primarySources, String[] args, Context context) { Enviorment enviorment = Aop.get(Enviorment.class); String env = enviorment.get(ConfigKeys.appEnv); if("dev".endsWith(env)) { TioBootArgument tioBootArgument = new TioBootArgument(primarySources, args, context, true); AopManager.me().addSingletonObject(tioBootArgument); if (hotSwapWatcher == null) { // 使用反射執(zhí)行下面的代碼 log.info("start hotSwapWatcher"); hotSwapWatcher = new HotSwapWatcher(new TioBootRestartServer()); hotSwapWatcher.start(); } } } }
在 afterStarted
方法中,如果處于開發(fā)環(huán)境,則啟動 HotSwapWatcher
。
5. 注冊服務器監(jiān)聽器
最后,編寫 TioBootServerListenerConfig
類來在啟動前將 TioBootServerListener
添加到 Aop 容器中:
package com.litongjava.tio.web.hello.config; import com.litongjava.jfinal.aop.annotation.Bean; import com.litongjava.jfinal.aop.annotation.BeforeStartConfiguration; import com.litongjava.tio.boot.server.TioBootServerListener; @BeforeStartConfiguration public class TioBootServerListenerConfig { @Bean public TioBootServerListener tioBootServerListener() { return new MyServerListener(); } }
這將確保 MyServerListener
能夠正確注冊并在應用啟動時被調用。
6.測試加載效果
如果是Eclipse IDE,保持一個文件即可測試加載效果,如果是IDEA環(huán)境需要再運行時手動編譯(Build-->Recompile)文件才可以看到效果
tio-boot文檔
https://litongjava.github.io/tio-boot-docs/
以上就是tio-boot整合hotswap-classloader實現(xiàn)熱加載方法實例的詳細內容,更多關于tio-boot hotswap-classloader熱加載的資料請關注腳本之家其它相關文章!
相關文章
從零搭建Spring Boot腳手架整合OSS作為文件服務器的詳細教程
這篇文章主要介紹了從零搭建Spring Boot腳手架整合OSS作為文件服務器的詳細教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08Spring?Security使用數(shù)據(jù)庫登錄認證授權
本文主要介紹了Spring?Security使用數(shù)據(jù)庫登錄認證授權,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01Maven中Could not find artifact XXXX的錯誤解決
本文主要介紹了Maven中Could not find artifact XXXX的錯誤解決,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03基于SpringAI+DeepSeek實現(xiàn)流式對話功能
一般來說大模型的響應速度通常是很慢的,為了避免用戶用戶能夠耐心等待輸出的結果,我們通常會使用流式輸出一點點將結果輸出給用戶,那么問題來了,想要實現(xiàn)流式結果輸出,后端和前端要如何配合?下來本文給出具體的實現(xiàn)代碼,需要的朋友可以參考下2025-02-02關于MyBatis 查詢數(shù)據(jù)時屬性中多對一的問題(多條數(shù)據(jù)對應一條數(shù)據(jù))
這篇文章主要介紹了MyBatis 查詢數(shù)據(jù)時屬性中多對一的問題(多條數(shù)據(jù)對應一條數(shù)據(jù)),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01