Class ServiceLoaderUtils
- java.lang.Object
-
- io.microsphere.util.ServiceLoaderUtils
-
- All Implemented Interfaces:
Utils
public abstract class ServiceLoaderUtils extends java.lang.Object implements Utils
Utility class for loading and managing service providers viaServiceLoader, with support for caching, prioritization, and ClassLoader hierarchy traversal.ServiceLoaderUtilsprovides methods to load all implementations of a service type, retrieve the first or last implementation based on declaration order or priority, and return them as either a list or an array. It supports custom class loaders and optional caching of loaded services.Key Features
- Load services using the context class loader or a specified class loader.
- Supports caching of loaded services (configurable).
- Sorts services by priority if they implement the
Prioritizedinterface. - Returns read-only lists or arrays of service instances.
Example Usage
Loading All Services
List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class); for (MyService service : services) { service.execute(); }Loading First Service (Highest Priority)
MyService service = ServiceLoaderUtils.loadFirstService(MyService.class); service.initialize();Loading Last Service (Lowest Priority)
MyService service = ServiceLoaderUtils.loadLastService(MyService.class); service.shutdown();Loading With Custom ClassLoader
ClassLoader cl = MyClassLoader.getInstance(); MyService[] services = ServiceLoaderUtils.loadServices(MyService.class, cl);Loading Without Caching
List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class, false);- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
ServiceLoader
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDEFAULT_SERVICE_LOADER_CACHED_PROPERTY_VALUEThe default value of theSERVICE_LOADER_CACHEDproperty :"false"static booleanSERVICE_LOADER_CACHEDWhether to cache the loaded servicesstatic java.lang.StringSERVICE_LOADER_CACHED_PROPERTY_NAMEThe name of theSERVICE_LOADER_CACHEDproperty :"microsphere.service-loader.cached"static java.lang.StringSERVICE_PROVIDER_CONFIG_FILES_LOCATION_PATTERNThe locatings' pattern of configuration files of the single service provider : "META-INF/services/{}", where "{}" is replaced with the service type name.static java.lang.StringSERVICES_PROVIDER_LOCATIONThe location of service provider configuration files : "META-INF/services/".
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> java.util.Set<java.lang.Class<T>>getServiceClasses(java.lang.Class<T> serviceType)Retrieves the implementation classes of all service providers for the specified service type using the context class loader.static <T> java.util.Set<java.lang.Class<T>>getServiceClasses(java.lang.Class<T> serviceType, boolean failFast)Retrieves the implementation classes of all service providers for the specified service type using the context class loader.static <T> java.util.Set<java.lang.Class<T>>getServiceClasses(java.lang.Class<T> serviceType, java.lang.ClassLoader classLoader)Retrieves the implementation classes of all service providers for the specified service type using the providedClassLoader.static <T> java.util.Set<java.lang.Class<T>>getServiceClasses(java.lang.Class<T> serviceType, java.lang.ClassLoader classLoader, boolean failFast)Retrieves the implementation classes of all service providers for the specified service type.static java.util.Set<java.lang.String>getServiceClassNames(java.lang.Class<?> serviceType)Retrieves the class names of all service providers for the specified service type using the context class loader.static java.util.Set<java.lang.String>getServiceClassNames(java.lang.Class<?> serviceType, java.lang.ClassLoader classLoader)Retrieves the class names of all service providers for the specified service type.static java.util.Set<java.net.URL>getServiceResoources(java.lang.Class<?> serviceType, java.lang.ClassLoader classLoader)Retrieves the URLs of the service provider configuration files for the specified service type.static <S> SloadFirstService(java.lang.Class<S> serviceType)Loads the first instance of the specifiedServiceLoaderservice type using the context class loader.static <S> SloadFirstService(java.lang.Class<S> serviceType, boolean cached)Loads the first instance of the specifiedServiceLoaderservice type using the context class loader, with an option to enable or disable caching of the loaded services.static <S> SloadFirstService(java.lang.Class<S> serviceType, java.lang.ClassLoader classLoader)Loads the first instance of the specifiedServiceLoaderservice type using the providedClassLoader.static <S> SloadFirstService(java.lang.Class<S> serviceType, java.lang.ClassLoader classLoader, boolean cached)Loads the first instance of the specifiedServiceLoaderservice type using the providedClassLoader, with an option to enable or disable caching of the loaded services.static <S> SloadLastService(java.lang.Class<S> serviceType)Loads the last instance of the specifiedServiceLoaderservice type using the context class loader.static <S> SloadLastService(java.lang.Class<S> serviceType, boolean cached)Loads the last instance of the specifiedServiceLoaderservice type using the context class loader, with an option to enable or disable caching of the loaded services.static <S> SloadLastService(java.lang.Class<S> serviceType, java.lang.ClassLoader classLoader)Loads the last instance of the specifiedServiceLoaderservice type using the providedClassLoader.static <S> SloadLastService(java.lang.Class<S> serviceType, java.lang.ClassLoader classLoader, boolean cached)Loads the last instance of the specifiedServiceLoaderservice type using the providedClassLoader, with an option to enable or disable caching of the loaded services.static <S> S[]loadServices(java.lang.Class<S> serviceType)Loads all implementation instances of the specifiedServiceLoaderservice type using the context class loader.static <S> S[]loadServices(java.lang.Class<S> serviceType, boolean cached)Loads all implementation instances of the specifiedServiceLoaderservice type using the context class loader, with an option to enable or disable caching of the loaded services.static <S> S[]loadServices(java.lang.Class<S> serviceType, java.lang.ClassLoader classLoader)Loads all implementation instances of the specifiedServiceLoaderservice type using the providedClassLoader.static <S> S[]loadServices(java.lang.Class<S> serviceType, java.lang.ClassLoader classLoader, boolean cached)Loads all implementation instances of the specifiedServiceLoaderservice type using the providedClassLoader, with an option to enable or disable caching of the loaded services.static <S> java.util.List<S>loadServicesList(java.lang.Class<S> serviceType)Loads all implementation instances of the specifiedServiceLoaderservice type using the context class loader.static <S> java.util.List<S>loadServicesList(java.lang.Class<S> serviceType, boolean cached)Loads all implementation instances of the specifiedServiceLoaderservice type using the context class loader, with an option to enable or disable caching of the loaded services.static <S> java.util.List<S>loadServicesList(java.lang.Class<S> serviceType, java.lang.ClassLoader classLoader)Loads all implementation instances of the specifiedServiceLoaderservice type using the providedClassLoader.static <S> java.util.List<S>loadServicesList(java.lang.Class<S> serviceType, java.lang.ClassLoader classLoader, boolean cached)Loads all implementation instances of the specifiedServiceLoaderservice type using the providedClassLoader, with an option to enable or disable caching of the loaded services.
-
-
-
Field Detail
-
DEFAULT_SERVICE_LOADER_CACHED_PROPERTY_VALUE
public static final java.lang.String DEFAULT_SERVICE_LOADER_CACHED_PROPERTY_VALUE
The default value of theSERVICE_LOADER_CACHEDproperty :"false"- See Also:
- Constant Field Values
-
SERVICE_LOADER_CACHED_PROPERTY_NAME
public static final java.lang.String SERVICE_LOADER_CACHED_PROPERTY_NAME
The name of theSERVICE_LOADER_CACHEDproperty :"microsphere.service-loader.cached"- See Also:
- Constant Field Values
-
SERVICES_PROVIDER_LOCATION
public static final java.lang.String SERVICES_PROVIDER_LOCATION
The location of service provider configuration files : "META-INF/services/".- See Also:
ServiceLoader, Constant Field Values
-
SERVICE_PROVIDER_CONFIG_FILES_LOCATION_PATTERN
public static final java.lang.String SERVICE_PROVIDER_CONFIG_FILES_LOCATION_PATTERN
The locatings' pattern of configuration files of the single service provider : "META-INF/services/{}", where "{}" is replaced with the service type name.- See Also:
ServiceLoader, Constant Field Values
-
SERVICE_LOADER_CACHED
@ConfigurationProperty(name="microsphere.service-loader.cached", defaultValue="false", description="Whether to cache the loaded services", source="system-properties") public static final boolean SERVICE_LOADER_CACHEDWhether to cache the loaded services
-
-
Method Detail
-
getServiceClasses
@Nonnull @Immutable public static <T> java.util.Set<java.lang.Class<T>> getServiceClasses(java.lang.Class<T> serviceType)
Retrieves the implementation classes of all service providers for the specified service type using the context class loader.This method searches for service provider configuration files located at
META-INF/services/<serviceType>using the context class loader associated with the service type. It loads the classes defined in these configuration files and verifies their assignability to the service type. This method uses the default fail-fast behavior (true).Example Usage
Set<Class<MyService>> classes = ServiceLoaderUtils.getServiceClasses(MyService.class); for (Class<MyService> clazz : classes) { System.out.println("Found service implementation class: " + clazz.getName()); }- Type Parameters:
T- the service type- Parameters:
serviceType- the class of the service type, cannot be null- Returns:
- an unmodifiable set of implementation classes of the service providers
- Throws:
java.lang.IllegalStateException- if a class cannot be loaded or is not assignable to the service type
-
getServiceClasses
@Nonnull @Immutable public static <T> java.util.Set<java.lang.Class<T>> getServiceClasses(java.lang.Class<T> serviceType, boolean failFast)Retrieves the implementation classes of all service providers for the specified service type using the context class loader.This method searches for service provider configuration files located at
META-INF/services/<serviceType>using the context class loader associated with the service type. It loads the classes defined in these configuration files and verifies their assignability to the service type.Example Usage
// Fail fast if a class cannot be loaded or is not assignable Set<Class<MyService>> classes = ServiceLoaderUtils.getServiceClasses(MyService.class, true); for (Class<MyService> clazz : classes) { System.out.println("Found service implementation class: " + clazz.getName()); } // Lenient mode: skip invalid classes Set<Class<MyService>> lenientClasses = ServiceLoaderUtils.getServiceClasses(MyService.class, false);- Type Parameters:
T- the service type- Parameters:
serviceType- the class of the service type, cannot be nullfailFast- iftrue, throws an exception when a class cannot be loaded or is not assignable; iffalse, skips invalid classes- Returns:
- an unmodifiable set of implementation classes of the service providers
- Throws:
java.lang.IllegalStateException- iffailFastistrueand an error occurs during loading or assignment check
-
getServiceClasses
@Nonnull @Immutable public static <T> java.util.Set<java.lang.Class<T>> getServiceClasses(java.lang.Class<T> serviceType, @Nullable java.lang.ClassLoader classLoader)Retrieves the implementation classes of all service providers for the specified service type using the providedClassLoader.This method searches for service provider configuration files located at
META-INF/services/<serviceType>using the providedClassLoader. It loads the classes defined in these configuration files and verifies their assignability to the service type. This method uses the default fail-fast behavior (true).Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Set<Class<MyService>> classes = ServiceLoaderUtils.getServiceClasses(MyService.class, classLoader); for (Class<MyService> clazz : classes) { System.out.println("Found service implementation class: " + clazz.getName()); }- Type Parameters:
T- the service type- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to locate and load the resources; may benull- Returns:
- an unmodifiable set of implementation classes of the service providers
- Throws:
java.lang.IllegalStateException- if a class cannot be loaded or is not assignable to the service type
-
getServiceClasses
@Nonnull @Immutable public static <T> java.util.Set<java.lang.Class<T>> getServiceClasses(java.lang.Class<T> serviceType, @Nullable java.lang.ClassLoader classLoader, boolean failFast)Retrieves the implementation classes of all service providers for the specified service type.This method searches for service provider configuration files located at
META-INF/services/<serviceType>using the providedClassLoader. It loads the classes defined in these configuration files and verifies their assignability to the service type.Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); // Fail fast if a class cannot be loaded or is not assignable Set<Class<MyService>> classes = ServiceLoaderUtils.getServiceClasses(MyService.class, classLoader, true); for (Class<MyService> clazz : classes) { System.out.println("Found service implementation class: " + clazz.getName()); } // Lenient mode: skip invalid classes Set<Class<MyService>> lenientClasses = ServiceLoaderUtils.getServiceClasses(MyService.class, classLoader, false);- Type Parameters:
T- the service type- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to locate and load the resources; may benullfailFast- iftrue, throws an exception when a class cannot be loaded or is not assignable; iffalse, skips invalid classes- Returns:
- an unmodifiable set of implementation classes of the service providers
- Throws:
java.lang.IllegalStateException- iffailFastistrueand an error occurs during loading or assignment check
-
getServiceClassNames
@Nonnull @Immutable public static java.util.Set<java.lang.String> getServiceClassNames(java.lang.Class<?> serviceType)
Retrieves the class names of all service providers for the specified service type using the context class loader.This method searches for service provider configuration files located at
META-INF/services/<serviceType>using the context class loader associated with the service type. It reads the fully qualified class names defined in these configuration files.Example Usage
Set<String> classNames = ServiceLoaderUtils.getServiceClassNames(MyService.class); for (String className : classNames) { System.out.println("Found service implementation: " + className); }- Parameters:
serviceType- the class of the service type, cannot be null- Returns:
- an unmodifiable set of fully qualified class names of the service providers
-
getServiceClassNames
@Nonnull @Immutable public static java.util.Set<java.lang.String> getServiceClassNames(java.lang.Class<?> serviceType, @Nullable java.lang.ClassLoader classLoader)Retrieves the class names of all service providers for the specified service type.This method searches for service provider configuration files located at
META-INF/services/<serviceType>using the providedClassLoader. It reads the fully qualified class names defined in these configuration files.Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Set<String> classNames = ServiceLoaderUtils.getServiceClassNames(MyService.class, classLoader); for (String className : classNames) { System.out.println("Found service implementation: " + className); }- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to locate the resources; may benull- Returns:
- an unmodifiable set of fully qualified class names of the service providers
-
getServiceResoources
@Nonnull @Immutable public static java.util.Set<java.net.URL> getServiceResoources(java.lang.Class<?> serviceType, @Nullable java.lang.ClassLoader classLoader) throws java.io.IOExceptionRetrieves the URLs of the service provider configuration files for the specified service type.This method searches for configuration files located at
META-INF/services/<serviceType>using the providedClassLoader. It traverses the class loader hierarchy to find all matching resources.Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); Set<URL> resources = ServiceLoaderUtils.getServiceResources(MyService.class, classLoader); for (URL url : resources) { System.out.println("Found service config at: " + url); }- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to locate the resources; may benull- Returns:
- a set of
URLs pointing to the service provider configuration files - Throws:
java.io.IOException- if an I/O error occurs while accessing the resources
-
loadServicesList
@Nonnull @Immutable public static <S> java.util.List<S> loadServicesList(java.lang.Class<S> serviceType) throws java.lang.IllegalArgumentExceptionLoads all implementation instances of the specifiedServiceLoaderservice type using the context class loader.This method traverses the hierarchy of
ClassLoaderto load the service configuration file located at/META-INF/services/<serviceType>. The returned list contains all discovered implementations in the order they were found, sorted by their priority if they implement thePrioritizedinterface.Example Usage
List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class); for (MyService service : services) { service.execute(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be null- Returns:
- a read-only list containing all implementation instances of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadServicesList
@Nonnull @Immutable public static <S> java.util.List<S> loadServicesList(java.lang.Class<S> serviceType, @Nullable java.lang.ClassLoader classLoader) throws java.lang.IllegalArgumentExceptionLoads all implementation instances of the specifiedServiceLoaderservice type using the providedClassLoader.This method traverses the hierarchy of
ClassLoaderto load the service configuration file located at/META-INF/services/<serviceType>. The returned list contains all discovered implementations in the order they were found, sorted by their priority if they implement thePrioritizedinterface.Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class, classLoader); for (MyService service : services) { service.execute(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to load service implementations; must not be null- Returns:
- a read-only list containing all implementation instances of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadServicesList
@Nonnull @Immutable public static <S> java.util.List<S> loadServicesList(java.lang.Class<S> serviceType, boolean cached) throws java.lang.IllegalArgumentExceptionLoads all implementation instances of the specifiedServiceLoaderservice type using the context class loader, with an option to enable or disable caching of the loaded services.This method traverses the hierarchy of
ClassLoaderto load the service configuration file located at/META-INF/services/<serviceType>. The returned list contains all discovered implementations in the order they were found, sorted by their priority if they implement thePrioritizedinterface.Example Usage
List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class, true); for (MyService service : services) { service.execute(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullcached- flag indicating whether to cache the loaded services- Returns:
- a read-only list containing all implementation instances of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadServicesList
@Nonnull @Immutable public static <S> java.util.List<S> loadServicesList(java.lang.Class<S> serviceType, @Nullable java.lang.ClassLoader classLoader, boolean cached) throws java.lang.IllegalArgumentExceptionLoads all implementation instances of the specifiedServiceLoaderservice type using the providedClassLoader, with an option to enable or disable caching of the loaded services.This method traverses the hierarchy of
ClassLoaderto load the service configuration file located at/META-INF/services/<serviceType>. The returned list contains all discovered implementations in the order they were found, sorted by their priority if they implement thePrioritizedinterface.Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class, classLoader, true); for (MyService service : services) { service.execute(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to load service implementations; must not be nullcached- flag indicating whether to cache the loaded services- Returns:
- a read-only list containing all implementation instances of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadServices
@Nonnull public static <S> S[] loadServices(java.lang.Class<S> serviceType) throws java.lang.IllegalArgumentExceptionLoads all implementation instances of the specifiedServiceLoaderservice type using the context class loader.This method traverses the hierarchy of
ClassLoaderto load the service configuration file located at/META-INF/services/<serviceType>. The returned array contains all discovered implementations in the order they were found, sorted by their priority if they implement thePrioritizedinterface.Example Usage
MyService[] services = ServiceLoaderUtils.loadServices(MyService.class); for (MyService service : services) { service.execute(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be null- Returns:
- an array containing all implementation instances of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadServices
@Nonnull public static <S> S[] loadServices(java.lang.Class<S> serviceType, @Nullable java.lang.ClassLoader classLoader) throws java.lang.IllegalArgumentExceptionLoads all implementation instances of the specifiedServiceLoaderservice type using the providedClassLoader.This method traverses the hierarchy of
ClassLoaderto load the service configuration file located at/META-INF/services/<serviceType>. The returned array contains all discovered implementations in the order they were found, sorted by their priority if they implement thePrioritizedinterface.Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); MyService[] services = ServiceLoaderUtils.loadServices(MyService.class, classLoader); for (MyService service : services) { service.execute(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to load service implementations; must not be null- Returns:
- an array containing all implementation instances of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadServices
@Nonnull public static <S> S[] loadServices(java.lang.Class<S> serviceType, boolean cached) throws java.lang.IllegalArgumentExceptionLoads all implementation instances of the specifiedServiceLoaderservice type using the context class loader, with an option to enable or disable caching of the loaded services.This method utilizes the context class loader associated with the provided service type to load the service configuration file located at
/META-INF/services/<serviceType>. The returned array contains all discovered implementations in the order they were found, sorted by their priority if they implement thePrioritizedinterface.Example Usage
MyService[] services = ServiceLoaderUtils.loadServices(MyService.class, true); for (MyService service : services) { service.initialize(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullcached- flag indicating whether to cache the loaded services- Returns:
- an array containing all implementation instances of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadServices
@Nonnull public static <S> S[] loadServices(java.lang.Class<S> serviceType, @Nullable java.lang.ClassLoader classLoader, boolean cached) throws java.lang.IllegalArgumentExceptionLoads all implementation instances of the specifiedServiceLoaderservice type using the providedClassLoader, with an option to enable or disable caching of the loaded services.This method traverses the hierarchy of
ClassLoaderto load the service configuration file located at/META-INF/services/<serviceType>. The returned array contains all discovered implementations in the order they were found, sorted by their priority if they implement thePrioritizedinterface.Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); MyService[] services = ServiceLoaderUtils.loadServices(MyService.class, classLoader, true); for (MyService service : services) { service.execute(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to load service implementations; must not be nullcached- flag indicating whether to cache the loaded services- Returns:
- an array containing all implementation instances of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadFirstService
@Nonnull public static <S> S loadFirstService(java.lang.Class<S> serviceType) throws java.lang.IllegalArgumentExceptionLoads the first instance of the specifiedServiceLoaderservice type using the context class loader.This method retrieves the list of service implementations using
loadServicesList(Class, ClassLoader), and returns the first element from the list. The order of service instances is determined by their declaration in the configuration files, and services implementing thePrioritizedinterface are sorted by priority.Example Usage
MyService service = ServiceLoaderUtils.loadFirstService(MyService.class); if (service != null) { service.execute(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be null- Returns:
- the first implementation instance of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadFirstService
@Nonnull public static <S> S loadFirstService(java.lang.Class<S> serviceType, boolean cached) throws java.lang.IllegalArgumentExceptionLoads the first instance of the specifiedServiceLoaderservice type using the context class loader, with an option to enable or disable caching of the loaded services.This method retrieves the list of service implementations using
loadServicesList(Class, ClassLoader, boolean), and returns the first element from the list. The order of service instances is determined by their declaration in the configuration files, and services implementing thePrioritizedinterface are sorted by priority.Design Purpose : Using the hierarchy of
ClassLoader, each level of ClassLoader will be able to access the configuration files under its class path /META-INF/services/serviceType. Then, override the first implementation class of the configuration file under the class path of ClassLoader, thereby providing a mechanism for overriding the implementation class.Example Usage
MyService service = ServiceLoaderUtils.loadFirstService(MyService.class, true); if (service != null) { service.initialize(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullcached- flag indicating whether to cache the loaded services- Returns:
- the first implementation instance of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadFirstService
@Nonnull public static <S> S loadFirstService(java.lang.Class<S> serviceType, @Nullable java.lang.ClassLoader classLoader) throws java.lang.IllegalArgumentExceptionLoads the first instance of the specifiedServiceLoaderservice type using the providedClassLoader.This method retrieves the list of service implementations using
loadServicesList(Class, ClassLoader), and returns the first element from the list. The order of service instances is determined by their declaration in the configuration files, and services implementing thePrioritizedinterface are sorted by priority.Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); MyService service = ServiceLoaderUtils.loadFirstService(MyService.class, classLoader); if (service != null) { service.execute(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to load service implementations; must not be null- Returns:
- the first implementation instance of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadFirstService
@Nonnull public static <S> S loadFirstService(java.lang.Class<S> serviceType, @Nullable java.lang.ClassLoader classLoader, boolean cached) throws java.lang.IllegalArgumentExceptionLoads the first instance of the specifiedServiceLoaderservice type using the providedClassLoader, with an option to enable or disable caching of the loaded services.This method retrieves the list of service implementations using
loadServicesList(Class, ClassLoader, boolean), and returns the first element from the list. The order of service instances is determined by their declaration in the configuration files, and services implementing thePrioritizedinterface are sorted by priority.Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); MyService service = ServiceLoaderUtils.loadFirstService(MyService.class, classLoader, true); if (service != null) { service.initialize(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to load service implementations; must not be nullcached- flag indicating whether to cache the loaded services- Returns:
- the first implementation instance of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadLastService
@Nonnull public static <S> S loadLastService(java.lang.Class<S> serviceType) throws java.lang.IllegalArgumentExceptionLoads the last instance of the specifiedServiceLoaderservice type using the context class loader.This method retrieves the list of service implementations using
loadServicesList(Class, ClassLoader), and returns the last element from the list. The order of service instances is determined by their declaration in the configuration files, and services implementing thePrioritizedinterface are sorted by priority.Example Usage
MyService service = ServiceLoaderUtils.loadLastService(MyService.class); if (service != null) { service.execute(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be null- Returns:
- the last implementation instance of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadLastService
@Nonnull public static <S> S loadLastService(java.lang.Class<S> serviceType, boolean cached) throws java.lang.IllegalArgumentExceptionLoads the last instance of the specifiedServiceLoaderservice type using the context class loader, with an option to enable or disable caching of the loaded services.This method retrieves the list of service implementations using
loadServicesList(Class, ClassLoader, boolean), and returns the last element from the list. The order of service instances is determined by their declaration in the configuration files, and services implementing thePrioritizedinterface are sorted by priority.Example Usage
MyService service = ServiceLoaderUtils.loadLastService(MyService.class, true); if (service != null) { service.initialize(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullcached- flag indicating whether to cache the loaded services- Returns:
- the last implementation instance of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadLastService
@Nonnull public static <S> S loadLastService(java.lang.Class<S> serviceType, @Nullable java.lang.ClassLoader classLoader) throws java.lang.IllegalArgumentExceptionLoads the last instance of the specifiedServiceLoaderservice type using the providedClassLoader.This method retrieves the list of service implementations using
loadServicesList(Class, ClassLoader), and returns the last element from the list. The order of service instances is determined by their declaration in the configuration files, and services implementing thePrioritizedinterface are sorted by priority.Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); MyService service = ServiceLoaderUtils.loadLastService(MyService.class, classLoader); if (service != null) { service.execute(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to load service implementations; must not be null- Returns:
- the last implementation instance of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
loadLastService
@Nonnull public static <S> S loadLastService(java.lang.Class<S> serviceType, @Nullable java.lang.ClassLoader classLoader, boolean cached) throws java.lang.IllegalArgumentExceptionLoads the last instance of the specifiedServiceLoaderservice type using the providedClassLoader, with an option to enable or disable caching of the loaded services.This method retrieves the list of service implementations using
loadServicesList(Class, ClassLoader, boolean), and returns the last element from the list. The order of service instances is determined by their declaration in the configuration files, and services implementing thePrioritizedinterface are sorted by priority.Example Usage
ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); MyService service = ServiceLoaderUtils.loadLastService(MyService.class, classLoader, true); if (service != null) { service.initialize(); }- Type Parameters:
S- the service type- Parameters:
serviceType- the class of the service type, cannot be nullclassLoader- the class loader used to load service implementations; must not be nullcached- flag indicating whether to cache the loaded services- Returns:
- the last implementation instance of the service type
- Throws:
java.lang.IllegalArgumentException- if no implementation is defined for the service type in the configuration file
-
-