spi service实现类加载代码

private static List<Factory> discoverFactories(ClassLoader classLoader) {
    //spi机制加载类
    final Iterator<Factory> serviceLoaderIterator =
            ServiceLoader.load(Factory.class, classLoader).iterator();

    final List<Factory> loadResults = new ArrayList<>();
    while (true) {
        try {
            // error handling should also be applied to the hasNext() call because service
            // loading might cause problems here as well
            if (!serviceLoaderIterator.hasNext()) {
                break;
            }

            loadResults.add(serviceLoaderIterator.next());
        } catch (Throwable t) {
            if (t instanceof NoClassDefFoundError) {
                LOG.debug(
                        "NoClassDefFoundError when loading a "
                                + Factory.class.getCanonicalName()
                                + ". This is expected when trying to load factory but no implementation is loaded.",
                        t);
            } else {
                throw new RuntimeException(
                        "Unexpected error when trying to load service provider.", t);
            }
        }
    }

    return loadResults;
}

相关推荐

  1. spi service实现代码

    2024-06-18 08:54:01       8 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-18 08:54:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-18 08:54:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-18 08:54:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-18 08:54:01       20 阅读

热门阅读

  1. 浅谈配置元件之TCP取样器配置/TCP取样器

    2024-06-18 08:54:01       7 阅读
  2. 算法 Hw9

    2024-06-18 08:54:01       8 阅读
  3. 6月17号

    2024-06-18 08:54:01       10 阅读
  4. Azure数据分析入门-发现数据分析

    2024-06-18 08:54:01       7 阅读
  5. Mac电脑安装配置NVM

    2024-06-18 08:54:01       7 阅读
  6. 架构设计 - 本地热点缓存

    2024-06-18 08:54:01       5 阅读
  7. 热门开源项目推荐

    2024-06-18 08:54:01       8 阅读
  8. 解析网络空间的安全威胁与应对

    2024-06-18 08:54:01       7 阅读
  9. 11、Spring之Bean生命周期~依赖注入(2)

    2024-06-18 08:54:01       8 阅读
  10. Go微服务: 悲观锁

    2024-06-18 08:54:01       5 阅读
  11. websocket nignx 配置

    2024-06-18 08:54:01       7 阅读