java jar热加载技术,spi~动态监控目录的jar实现热加载

对于我们自己封装的spi来说,我们可能希望他实现类似于插件的功能,例如你有一个汽车工厂,你目前有提供小汽车,如果你希望他动态支持卡车,公交车,那么spi可以帮你实现这个功能,对于我实现这个SPI功能主要由以下几个步骤组成。

对文件夹目录的监控

对文件夹里jar也的装载,动态类加载器机制实现

通过类型名称,返回实现类的列表

具体实现

目录监控

/**

* 目录监控.

*

* @param path

*/

public static void watchDir(String path) {

initClassLoader(path);

try (WatchService watchService = FileSystems.getDefault().newWatchService()) {

//给path路径加上文件观察服务

Paths.get(path).register(watchService, StandardWatchEventKinds.ENTRY_CREATE,

StandardWatchEventKinds.ENTRY_MODIFY,

StandardWatchEventKinds.ENTRY_DELETE);

while (true) {

final WatchKey key = watchService.take();

for (WatchEvent> watchEvent : key.pollEvents()) {

final WatchEvent.Kind> kind = watchEvent.kind();

if (kind == StandardWatchEventKinds.OVERFLOW) {

continue;

}

final WatchEvent watchEventPath = (WatchEvent) watchEvent;

final Path filename = watchEventPath.context();

System.out.println(kind + " -> " + filename);

initClassLoader(path);

}

boolean valid = key.reset();

if (!valid) {

break;

}

}

} catch (IOException | InterruptedException ex) {

System.err.println(ex);

}

}

目录下动态类加载器添加到当前系统加载器里

static void initClassLoader(String path) {

for (File file : FileUtil.loopFiles(path)) {

System.out.println("load jar:" + file.getName());

URL url = file.toURI().toURL();

DynamicClassLoader dynamicClassLoader = new DynamicClassLoader(new URL[]{url}, ClassLoader.getSystemClassLoader());

dynamicClassLoaders.add(dynamicClassLoader);

}

}

通过类型返回类型的实现

/**

* 返回所有具体的providerFactory工厂,使用dynamicClassLoaders加载器

*

* @param clazz

* @param

* @return

*/

public static List getProviderFactory(Class clazz) {

List list = new ArrayList<>();

for (ClassLoader classLoader : dynamicClassLoaders) {

ServiceLoader load = ServiceLoader.load(clazz, classLoader);

List idList = list.stream().map(o -> o.getId()).collect(Collectors.toList());

for (U providerFactory : load) {

if (!idList.contains(providerFactory.getId())) {

list.add(providerFactory);

}

}

}

return list;

}

程序调用

9c9a7842cded680cb2623c5984700c30.png

@SneakyThrows

@GetMapping("hello")

public ResponseEntity hello() {

List result = new ArrayList<>();

for (ProviderFactory u : SpiFactory.getProviderFactory(ProviderFactory.class)) {

result.add(u.create().login());

}

return ResponseEntity.ok(result);

}

结果

e1aeab37bfc36430ca005a5eaa51eb34.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值