Class SpiLoader

java.lang.Object
cloud.opencode.base.core.spi.SpiLoader

public final class SpiLoader extends Object
SPI Loader - Service Provider Interface loading utility SPI 加载器 - 服务提供者接口加载工具

Provides cached SPI service loading using Java ServiceLoader mechanism.

使用 Java ServiceLoader 机制提供带缓存的 SPI 服务加载。

Features | 主要功能:

  • Load all implementations (load) - 加载所有实现
  • Load first implementation (loadFirst) - 加载第一个实现
  • Load with default (loadFirstOrDefault) - 带默认值加载
  • Filter by type (loadByType) - 按类型过滤
  • Cached loading for performance - 缓存加载提升性能
  • Reload support (reload) - 重新加载支持

Usage Examples | 使用示例:

List<MyService> services = SpiLoader.load(MyService.class);
Optional<MyService> first = SpiLoader.loadFirst(MyService.class);
MyService service = SpiLoader.loadFirstOrDefault(MyService.class, defaultImpl);

if (SpiLoader.hasService(MyService.class)) {
    int count = SpiLoader.count(MyService.class);
}

Security | 安全性:

  • Thread-safe: Yes (ConcurrentHashMap) - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-core V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    Clears the cache 清除缓存
    static void
    clearCache(Class<?> serviceClass)
    Clears the cache for the specified class 清除指定类的缓存
    static <T> int
    count(Class<T> serviceClass)
    Gets the SPI service count 获取 SPI 服务数量
    static <T> boolean
    hasService(Class<T> serviceClass)
    Checks if the SPI service exists 检查 SPI 服务是否存在
    static <T> List<T>
    load(Class<T> serviceClass)
    Loads SPI service implementations 加载 SPI 服务实现
    static <T> List<T>
    load(Class<T> serviceClass, ClassLoader classLoader)
    Loads SPI service implementations (with specified ClassLoader) 加载 SPI 服务实现(指定类加载器)
    static <T, S extends T>
    List<S>
    loadByType(Class<T> serviceClass, Class<S> targetType)
    Filters SPI services by type 按类型过滤 SPI 服务
    static <T> Optional<T>
    loadFirst(Class<T> serviceClass)
    Loads the first SPI service implementation 加载第一个 SPI 服务实现
    static <T> T
    loadFirstOrDefault(Class<T> serviceClass, T defaultValue)
    Loads the first SPI service implementation, or uses the default 加载第一个 SPI 服务实现,不存在则使用默认值
    static <T> Stream<T>
    loadStream(Class<T> serviceClass)
    Lazily loads SPI service implementations as a Stream without eagerly materializing.
    static <T> Stream<T>
    loadStream(Class<T> serviceClass, ClassLoader classLoader)
    Lazily loads SPI service implementations as a Stream with a specified ClassLoader.
    static <T> List<T>
    reload(Class<T> serviceClass)
    Forces reload of SPI services 强制重新加载 SPI 服务

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • load

      public static <T> List<T> load(Class<T> serviceClass)
      Loads SPI service implementations 加载 SPI 服务实现
    • load

      public static <T> List<T> load(Class<T> serviceClass, ClassLoader classLoader)
      Loads SPI service implementations (with specified ClassLoader) 加载 SPI 服务实现(指定类加载器)
    • loadFirst

      public static <T> Optional<T> loadFirst(Class<T> serviceClass)
      Loads the first SPI service implementation 加载第一个 SPI 服务实现
    • loadFirstOrDefault

      public static <T> T loadFirstOrDefault(Class<T> serviceClass, T defaultValue)
      Loads the first SPI service implementation, or uses the default 加载第一个 SPI 服务实现,不存在则使用默认值
    • reload

      public static <T> List<T> reload(Class<T> serviceClass)
      Forces reload of SPI services 强制重新加载 SPI 服务
    • hasService

      public static <T> boolean hasService(Class<T> serviceClass)
      Checks if the SPI service exists 检查 SPI 服务是否存在
    • count

      public static <T> int count(Class<T> serviceClass)
      Gets the SPI service count 获取 SPI 服务数量
    • loadByType

      public static <T, S extends T> List<S> loadByType(Class<T> serviceClass, Class<S> targetType)
      Filters SPI services by type 按类型过滤 SPI 服务
    • loadStream

      public static <T> Stream<T> loadStream(Class<T> serviceClass)
      Lazily loads SPI service implementations as a Stream without eagerly materializing. 延迟加载 SPI 服务实现为 Stream,不会立即实例化所有实现。
      Type Parameters:
      T - the service type - 服务类型
      Parameters:
      serviceClass - the service class - 服务类
      Returns:
      a stream of service implementations - 服务实现的流
    • loadStream

      public static <T> Stream<T> loadStream(Class<T> serviceClass, ClassLoader classLoader)
      Lazily loads SPI service implementations as a Stream with a specified ClassLoader. 使用指定类加载器延迟加载 SPI 服务实现为 Stream。
      Type Parameters:
      T - the service type - 服务类型
      Parameters:
      serviceClass - the service class - 服务类
      classLoader - the class loader - 类加载器
      Returns:
      a stream of service implementations - 服务实现的流
    • clearCache

      public static void clearCache()
      Clears the cache 清除缓存
    • clearCache

      public static void clearCache(Class<?> serviceClass)
      Clears the cache for the specified class 清除指定类的缓存