Class SpiLoader
Providers are discovered once for each service type and effective thread
context classloader. The returned list is immutable and retains provider
order from ServiceLoader. Both cache-key referents and cached
snapshots are weak: an application redeploy can reclaim its classloader even
when this utility was loaded by a parent classloader.
A snapshot may be rediscovered after it becomes otherwise unreachable and
is reclaimed. Callers that need deterministic refresh should use
invalidate(Class) or invalidateAll().
- Since:
- 0.14.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional<T> Returns the first provider visible to the effective thread context classloader without constructing later providers.static <T> Optional<T> findFirst(Class<T> serviceType, ClassLoader classLoader) Returns the first provider visible toclassLoaderwithout constructing later providers.static <T> voidinvalidate(Class<T> serviceType) Invalidates every classloader-scoped snapshot forserviceType.static voidClears every cached provider snapshot.static <T> List<T> Returns the cached providers visible to the effective thread context classloader.static <T> List<T> load(Class<T> serviceType, ClassLoader classLoader) Returns the cached providers visible toclassLoader.
-
Method Details
-
load
Returns the cached providers visible to the effective thread context classloader.If the current thread has no context classloader, the service type's defining classloader is used, followed by the system classloader for a bootstrap-defined service type.
- Type Parameters:
T- service type- Parameters:
serviceType- service interface or abstract class- Returns:
- immutable providers in
ServiceLoaderorder - Throws:
ServiceConfigurationError- when provider discovery fails
-
findFirst
Returns the first provider visible to the effective thread context classloader without constructing later providers.This preserves
ServiceLoader.findFirst()semantics for APIs that select a single provider while still caching the result.- Type Parameters:
T- service type- Parameters:
serviceType- service interface or abstract class- Returns:
- cached first provider, or empty when none is registered
- Throws:
ServiceConfigurationError- when first-provider discovery fails
-
findFirst
Returns the first provider visible toclassLoaderwithout constructing later providers.- Type Parameters:
T- service type- Parameters:
serviceType- service interface or abstract classclassLoader- provider classloader- Returns:
- cached first provider, or empty when none is registered
- Throws:
ServiceConfigurationError- when first-provider discovery fails
-
load
Returns the cached providers visible toclassLoader.- Type Parameters:
T- service type- Parameters:
serviceType- service interface or abstract classclassLoader- provider classloader- Returns:
- immutable providers in
ServiceLoaderorder - Throws:
ServiceConfigurationError- when provider discovery fails
-
invalidate
Invalidates every classloader-scoped snapshot forserviceType. The next load performs fresh discovery.- Type Parameters:
T- service type- Parameters:
serviceType- service interface or abstract class
-
invalidateAll
public static void invalidateAll()Clears every cached provider snapshot.
-