Class SpiLoader
java.lang.Object
cloud.opencode.base.core.spi.SpiLoader
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 TypeMethodDescriptionstatic voidClears the cache 清除缓存static voidclearCache(Class<?> serviceClass) Clears the cache for the specified class 清除指定类的缓存static <T> intGets the SPI service count 获取 SPI 服务数量static <T> booleanhasService(Class<T> serviceClass) Checks if the SPI service exists 检查 SPI 服务是否存在static <T> List<T> 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> Loads the first SPI service implementation 加载第一个 SPI 服务实现static <T> TloadFirstOrDefault(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> Forces reload of SPI services 强制重新加载 SPI 服务
-
Method Details
-
load
-
load
Loads SPI service implementations (with specified ClassLoader) 加载 SPI 服务实现(指定类加载器) -
loadFirst
-
loadFirstOrDefault
Loads the first SPI service implementation, or uses the default 加载第一个 SPI 服务实现,不存在则使用默认值 -
reload
-
hasService
Checks if the SPI service exists 检查 SPI 服务是否存在 -
count
Gets the SPI service count 获取 SPI 服务数量 -
loadByType
-
loadStream
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
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
Clears the cache for the specified class 清除指定类的缓存
-