public final class SpiLoader<S> extends Object
SPI is short for Service Provider Interface.
Service is represented by a single type, that is, a single interface or an abstract class. Provider is implementations of Service, that is, some classes which implement the interface or extends the abstract class.
For Service type: Must interface or abstract class.
For Provider class: Must have a zero-argument constructor so that they can be instantiated during loading.
For Provider configuration file: 1. The file contains a list of fully-qualified binary names of concrete provider classes, one per line. 2. Space and tab characters surrounding each name, as well as blank lines, are ignored. 3. The comment line character is #, all characters following it are ignored.
SpiLoader provide common functions, such as:
Spi,
ServiceLoader| Modifier and Type | Method and Description |
|---|---|
void |
load()
Load the Provider class from Provider configuration file
|
S |
loadDefaultInstance()
Load default Provider instance
Provider class with @Spi(isDefault = true)
|
S |
loadFirstInstance()
Load the first-found Provider instance
|
S |
loadFirstInstanceOrDefault()
Load the first-found Provider instance,if not found, return default Provider instance
|
S |
loadHighestPriorityInstance()
Load highest order priority instance, order value is defined in class's
Spi annotation |
S |
loadInstance(Class<? extends S> clazz)
Load instance by specific class type
|
S |
loadInstance(String aliasName)
Load instance by aliasName of Provider class
|
List<S> |
loadInstanceList()
Load all Provider instances of the specified Service
|
List<S> |
loadInstanceListSorted()
Load all Provider instances of the specified Service, sorted by order value in class's
Spi annotation |
S |
loadLowestPriorityInstance()
Load lowest order priority instance, order value is defined in class's
Spi annotation |
static <T> SpiLoader<T> |
of(Class<T> service)
Create SpiLoader instance via Service class
Cached by className, and load from cache first
|
String |
toString() |
public static <T> SpiLoader<T> of(Class<T> service)
T - Service typeservice - Service classpublic List<S> loadInstanceList()
public List<S> loadInstanceListSorted()
Spi annotationpublic S loadHighestPriorityInstance()
Spi annotationpublic S loadLowestPriorityInstance()
Spi annotationpublic S loadFirstInstance()
public S loadFirstInstanceOrDefault()
public S loadDefaultInstance()
public S loadInstance(Class<? extends S> clazz)
clazz - class typepublic S loadInstance(String aliasName)
aliasName - aliasName of Provider classpublic void load()
Copyright © 2021 Alibaba Group. All rights reserved.