Class ServiceBridge
java.lang.Object
cloud.opencode.base.classloader.service.ServiceBridge
Service Bridge - Cross-ClassLoader service discovery utility
服务桥接 - 跨类加载器服务发现工具
Discovers service implementations across multiple ClassLoaders using
ServiceLoader, orders them by @Priority annotation value,
and returns them as ServiceEntry records.
通过 ServiceLoader 在多个类加载器中发现服务实现,
按 @Priority 注解值排序,以 ServiceEntry 记录返回。
Features | 主要功能:
- Load services across multiple ClassLoaders - 跨多个类加载器加载服务
- Priority-based ordering via @Priority annotation - 基于 @Priority 注解的优先级排序
- Supports both javax.annotation.Priority and jakarta.annotation.Priority - 支持 javax 和 jakarta 两种 Priority 注解
- Graceful error handling (logs and skips failures) - 优雅的错误处理(记录并跳过失败)
Usage Examples | 使用示例:
// Load all implementations across classloaders
List<ServiceEntry<MyService>> entries = ServiceBridge.load(
MyService.class, cl1, cl2, cl3);
// Get the highest-priority implementation
Optional<MyService> best = ServiceBridge.loadFirst(
MyService.class, cl1, cl2);
Security | 安全性:
- Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
- Does not hold strong ClassLoader references - 不持有类加载器的强引用
- Since:
- JDK 25, opencode-base-classloader V1.0.3
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <S> List<ServiceEntry<S>> load(Class<S> serviceType, ClassLoader... classLoaders) Load services of the given type from multiple ClassLoaders.static <S> List<ServiceEntry<S>> load(Class<S> serviceType, Collection<ClassLoader> classLoaders) Load services of the given type from a collection of ClassLoaders.static <S> Optional<S> loadFirst(Class<S> serviceType, ClassLoader... classLoaders) Load the highest-priority service of the given type from multiple ClassLoaders.
-
Method Details
-
load
Load services of the given type from multiple ClassLoaders. 从多个类加载器中加载指定类型的服务。- Type Parameters:
S- the service type | 服务类型- Parameters:
serviceType- the service interface or abstract class | 服务接口或抽象类classLoaders- the ClassLoaders to search | 要搜索的类加载器- Returns:
- sorted list of service entries (by priority ascending) | 按优先级升序排序的服务条目列表
- Throws:
NullPointerException- if serviceType or classLoaders is null | 如果参数为 null 则抛出空指针异常
-
load
public static <S> List<ServiceEntry<S>> load(Class<S> serviceType, Collection<ClassLoader> classLoaders) Load services of the given type from a collection of ClassLoaders. 从类加载器集合中加载指定类型的服务。- Type Parameters:
S- the service type | 服务类型- Parameters:
serviceType- the service interface or abstract class | 服务接口或抽象类classLoaders- the ClassLoaders to search | 要搜索的类加载器- Returns:
- sorted list of service entries (by priority ascending) | 按优先级升序排序的服务条目列表
- Throws:
NullPointerException- if serviceType or classLoaders is null | 如果参数为 null 则抛出空指针异常
-
loadFirst
Load the highest-priority service of the given type from multiple ClassLoaders. 从多个类加载器中加载最高优先级的服务。- Type Parameters:
S- the service type | 服务类型- Parameters:
serviceType- the service interface or abstract class | 服务接口或抽象类classLoaders- the ClassLoaders to search | 要搜索的类加载器- Returns:
- the highest-priority service, or empty if none found | 最高优先级的服务,若未找到则为空
- Throws:
NullPointerException- if serviceType or classLoaders is null | 如果参数为 null 则抛出空指针异常
-