SpringBoot使用PropertiesLauncher加載外部jar包
啟用SpringBoot的PropertiesLauncher
使用SpringBoot的PropertiesLauncher可以優(yōu)先加載外部的jar文件, 這樣可以在程序運(yùn)行前替換jar包,
官方文檔: Launching Executable Jars
使用演示
建立一個(gè)SpringBoot工程, 工程中依賴一個(gè)叫自定義的utils包, 版本是1.0.0, 通過(guò)http接口返回utils版本, 正常打包后訪問(wèn), 返回1.0.0版本
@Slf4j
@RestController
public class HelloController {
@RequestMapping("/version")
public String version() {
String version = VersionUtil.getVersion();
log.info("請(qǐng)求version: " + version);
return VersionUtil.getVersion();
}
@RequestMapping("spi-version")
public Object spiVersion() {
ArrayList<String> objects = new ArrayList<>();
ServiceLoader<AgentInterface> load = ServiceLoader.load(AgentInterface.class);
for (AgentInterface registry : load) {
objects.add(registry.hello());
}
return objects;
}
@RequestMapping("/spring-res")
public Object springRes() throws IOException {
ArrayList<String> objects = new ArrayList<>();
PathMatchingResourcePatternResolver pathMatchingResourcePatternResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = pathMatchingResourcePatternResolver.getResources("classpath*:META-INF/services/com.example.utils.AgentInterface");
for (Resource resource : resources) {
objects.add(resource.toString());
}
return objects;
}
}2.在啟用應(yīng)用程序時(shí)通過(guò)-Dloader.path=/libs指定外部jar的目錄, 再啟動(dòng), 訪問(wèn)接口返回2.0.0版本, 說(shuō)明包替換成功
指定PropertiesLauncher啟動(dòng)類執(zhí)行
java -cp demo1-0.0.1-SNAPSHOT.jar org.springframework.boot.loader.PropertiesLauncher

java -cp demo1-0.0.1-SNAPSHOT.jar -Dloader.path=/Users/admin/.m2/repository/com/example/utils/2.0.0/ org.springframework.boot.loader.PropertiesLauncher

訪問(wèn)java spi
可以正常只加載2.0.0版本中的實(shí)現(xiàn)類, 這個(gè)符合預(yù)期

訪問(wèn)資源文件
訪問(wèn)資源文件, 會(huì)發(fā)現(xiàn)本應(yīng)只從2.0.0版本中加載文件, 結(jié)果1.0.0版本中的也被加載了

到此這篇關(guān)于SpringBoot使用PropertiesLauncher加載外部jar包的文章就介紹到這了,更多相關(guān)SpringBoot加載外部jar包內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringSecurity6.x多種登錄方式配置小結(jié)
SpringSecurity6.x變了很多寫法,本文就來(lái)介紹一下SpringSecurity6.x多種登錄方式配置小結(jié),具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
SpringBoot @Autowired注解注入規(guī)則介紹
這篇文章主要介紹了SpringBoot @Autowired注解注入規(guī)則介紹,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2021-11-11
Java關(guān)于含有繼承類的成員初始化過(guò)程講解
今天小編就為大家分享一篇Java關(guān)于含有繼承類的成員初始化過(guò)程講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04
Java?@Autowired報(bào)錯(cuò)原因分析和4種解決方案
這篇文章主要介紹了Java?@Autowired報(bào)錯(cuò)原因分析和4種解決方案,文章圍繞主題展開(kāi)詳細(xì)內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考以一下2022-05-05
Java中時(shí)間戳和時(shí)間的轉(zhuǎn)換方法代碼
這篇文章主要介紹了Java中時(shí)間戳和時(shí)間的轉(zhuǎn)換的相關(guān)資料,Java8中時(shí)間戳與日期時(shí)間對(duì)象之間的轉(zhuǎn)換是編程中常見(jiàn)的操作,通過(guò)時(shí)間字符串獲取時(shí)間對(duì)象也是其中的一種方法,需要的朋友可以參考下2025-03-03
SpringBoot+Redis防止惡意刷新與暴力請(qǐng)求接口的實(shí)現(xiàn)
這篇文章主要為大家介紹了如何利用springboot和Redis來(lái)實(shí)現(xiàn)防止惡意刷新與暴力請(qǐng)求接口,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06

