Skip to content

io microsphere util ServiceLoaderUtils

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

ServiceLoaderUtils

Type: Class | Module: microsphere-java-core | Package: io.microsphere.util | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/util/ServiceLoaderUtils.java

Overview

Utility class for loading and managing service providers via ServiceLoader, with support for caching, prioritization, and ClassLoader hierarchy traversal.

ServiceLoaderUtils provides 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 `io.microsphere.lang.Prioritized` interface.
- Returns read-only lists or arrays of service instances.

Example Usage

Loading All Services

`List 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 services = ServiceLoaderUtils.loadServicesList(MyService.class, false);
`

Declaration

public abstract class ServiceLoaderUtils implements Utils

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

Example 1

List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class);
for (MyService service : services) {
    service.execute();
}

Example 2

MyService service = ServiceLoaderUtils.loadFirstService(MyService.class);
service.initialize();

Example 3

MyService service = ServiceLoaderUtils.loadLastService(MyService.class);
service.shutdown();

Example 4

ClassLoader cl = MyClassLoader.getInstance();
MyService[] services = ServiceLoaderUtils.loadServices(MyService.class, cl);

Example 5

List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class, false);

Method Examples

loadServicesList

List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class);
for (MyService service : services) {
    service.execute();
}

loadServicesList

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class, classLoader);
for (MyService service : services) {
    service.execute();
}

loadServicesList

List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class, true);
for (MyService service : services) {
    service.execute();
}

loadServicesList

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
List<MyService> services = ServiceLoaderUtils.loadServicesList(MyService.class, classLoader, true);
for (MyService service : services) {
    service.execute();
}

loadServices

MyService[] services = ServiceLoaderUtils.loadServices(MyService.class);
for (MyService service : services) {
    service.execute();
}

loadServices

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService[] services = ServiceLoaderUtils.loadServices(MyService.class, classLoader);
for (MyService service : services) {
    service.execute();
}

loadServices

MyService[] services = ServiceLoaderUtils.loadServices(MyService.class, true);
for (MyService service : services) {
    service.initialize();
}

loadServices

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService[] services = ServiceLoaderUtils.loadServices(MyService.class, classLoader, true);
for (MyService service : services) {
    service.execute();
}

loadFirstService

MyService service = ServiceLoaderUtils.loadFirstService(MyService.class);
if (service != null) {
    service.execute();
}

loadFirstService

MyService service = ServiceLoaderUtils.loadFirstService(MyService.class, true);
if (service != null) {
    service.initialize();
}

loadFirstService

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService service = ServiceLoaderUtils.loadFirstService(MyService.class, classLoader);
if (service != null) {
    service.execute();
}

loadFirstService

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService service = ServiceLoaderUtils.loadFirstService(MyService.class, classLoader, true);
if (service != null) {
    service.initialize();
}

loadLastService

MyService service = ServiceLoaderUtils.loadLastService(MyService.class);
if (service != null) {
    service.execute();
}

loadLastService

MyService service = ServiceLoaderUtils.loadLastService(MyService.class, true);
if (service != null) {
    service.initialize();
}

loadLastService

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService service = ServiceLoaderUtils.loadLastService(MyService.class, classLoader);
if (service != null) {
    service.execute();
}

loadLastService

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService service = ServiceLoaderUtils.loadLastService(MyService.class, classLoader, true);
if (service != null) {
    service.initialize();
}

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.util.ServiceLoaderUtils;

API Reference

Public Methods

Method Description
loadServicesList The default value of the #SERVICE_LOADER_CACHED property : "false"
loadServicesList Loads all implementation instances of the specified ServiceLoader service type using the provided ClassLoader.
loadServicesList Loads all implementation instances of the specified ServiceLoader service type using the context class loader,
loadServicesList Loads all implementation instances of the specified ServiceLoader service type using the provided ClassLoader,
loadServices Loads all implementation instances of the specified ServiceLoader service type using the context class loader.
loadServices Loads all implementation instances of the specified ServiceLoader service type using the provided ClassLoader.
loadServices Loads all implementation instances of the specified ServiceLoader service type using the context class loader,
loadServices Loads all implementation instances of the specified ServiceLoader service type using the provided ClassLoader,
loadFirstService Loads the first instance of the specified ServiceLoader service type using the context class loader.
loadFirstService Loads the first instance of the specified ServiceLoader service type using the context class loader,
loadFirstService Loads the first instance of the specified ServiceLoader service type using the provided ClassLoader.
loadFirstService Loads the first instance of the specified ServiceLoader service type using the provided ClassLoader,
loadLastService Loads the last instance of the specified ServiceLoader service type using the context class loader.
loadLastService Loads the last instance of the specified ServiceLoader service type using the context class loader,
loadLastService Loads the last instance of the specified ServiceLoader service type using the provided ClassLoader.
loadLastService Loads the last instance of the specified ServiceLoader service type using the provided ClassLoader,

Method Details

loadServicesList

public static <S> List<S> loadServicesList(Class<S> serviceType)

The default value of the #SERVICE_LOADER_CACHED property : "false" / public static final String DEFAULT_SERVICE_LOADER_CACHED_PROPERTY_VALUE = "false";

/** The name of the #SERVICE_LOADER_CACHED property : "microsphere.service-loader.cached" / public static final String SERVICE_LOADER_CACHED_PROPERTY_NAME = MICROSPHERE_PROPERTY_NAME_PREFIX + "service-loader.cached";

/** Whether to cache the loaded services /

loadServicesList

public static <S> List<S> loadServicesList(Class<S> serviceType, @Nullable ClassLoader classLoader)

Loads all implementation instances of the specified ServiceLoader service type using the provided ClassLoader.

This method traverses the hierarchy of ClassLoader to load the service configuration file located at /META-INF/services/. The returned list contains all discovered implementations in the order they were found, sorted by their priority if they implement the io.microsphere.lang.Prioritized interface.

Example Usage

`ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
List services = ServiceLoaderUtils.loadServicesList(MyService.class, classLoader);
for (MyService service : services) {
    service.execute();
`
}

loadServicesList

public static <S> List<S> loadServicesList(Class<S> serviceType, boolean cached)

Loads all implementation instances of the specified ServiceLoader service type using the context class loader, with an option to enable or disable caching of the loaded services.

This method traverses the hierarchy of ClassLoader to load the service configuration file located at /META-INF/services/. The returned list contains all discovered implementations in the order they were found, sorted by their priority if they implement the io.microsphere.lang.Prioritized interface.

Example Usage

`List services = ServiceLoaderUtils.loadServicesList(MyService.class, true);
for (MyService service : services) {
    service.execute();
`
}

loadServicesList

public static <S> List<S> loadServicesList(Class<S> serviceType, @Nullable ClassLoader classLoader, boolean cached)

Loads all implementation instances of the specified ServiceLoader service type using the provided ClassLoader, with an option to enable or disable caching of the loaded services.

This method traverses the hierarchy of ClassLoader to load the service configuration file located at /META-INF/services/. The returned list contains all discovered implementations in the order they were found, sorted by their priority if they implement the io.microsphere.lang.Prioritized interface.

Example Usage

`ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
List services = ServiceLoaderUtils.loadServicesList(MyService.class, classLoader, true);
for (MyService service : services) {
    service.execute();
`
}

loadServices

public static <S> S[] loadServices(Class<S> serviceType)

Loads all implementation instances of the specified ServiceLoader service type using the context class loader.

This method traverses the hierarchy of ClassLoader to load the service configuration file located at /META-INF/services/. The returned array contains all discovered implementations in the order they were found, sorted by their priority if they implement the io.microsphere.lang.Prioritized interface.

Example Usage

`MyService[] services = ServiceLoaderUtils.loadServices(MyService.class);
for (MyService service : services) {
    service.execute();
`
}

loadServices

public static <S> S[] loadServices(Class<S> serviceType, @Nullable ClassLoader classLoader)

Loads all implementation instances of the specified ServiceLoader service type using the provided ClassLoader.

This method traverses the hierarchy of ClassLoader to load the service configuration file located at /META-INF/services/. The returned array contains all discovered implementations in the order they were found, sorted by their priority if they implement the io.microsphere.lang.Prioritized interface.

Example Usage

`ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService[] services = ServiceLoaderUtils.loadServices(MyService.class, classLoader);
for (MyService service : services) {
    service.execute();
`
}

loadServices

public static <S> S[] loadServices(Class<S> serviceType, boolean cached)

Loads all implementation instances of the specified ServiceLoader service 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/. The returned array contains all discovered implementations in the order they were found, sorted by their priority if they implement the io.microsphere.lang.Prioritized interface.

Example Usage

`MyService[] services = ServiceLoaderUtils.loadServices(MyService.class, true);
for (MyService service : services) {
    service.initialize();
`
}

loadServices

public static <S> S[] loadServices(Class<S> serviceType, @Nullable ClassLoader classLoader, boolean cached)

Loads all implementation instances of the specified ServiceLoader service type using the provided ClassLoader, with an option to enable or disable caching of the loaded services.

This method traverses the hierarchy of ClassLoader to load the service configuration file located at /META-INF/services/. The returned array contains all discovered implementations in the order they were found, sorted by their priority if they implement the io.microsphere.lang.Prioritized interface.

Example Usage

`ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService[] services = ServiceLoaderUtils.loadServices(MyService.class, classLoader, true);
for (MyService service : services) {
    service.execute();
`
}

loadFirstService

public static <S> S loadFirstService(Class<S> serviceType)

Loads the first instance of the specified ServiceLoader service 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 the io.microsphere.lang.Prioritized interface are sorted by priority.

Example Usage

`MyService service = ServiceLoaderUtils.loadFirstService(MyService.class);
if (service != null) {
    service.execute();
`
}

loadFirstService

public static <S> S loadFirstService(Class<S> serviceType, boolean cached)

Loads the first instance of the specified ServiceLoader service 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 the io.microsphere.lang.Prioritized interface 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();
`
}

loadFirstService

public static <S> S loadFirstService(Class<S> serviceType, @Nullable ClassLoader classLoader)

Loads the first instance of the specified ServiceLoader service type using the provided ClassLoader.

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 the io.microsphere.lang.Prioritized interface are sorted by priority.

Example Usage

`ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService service = ServiceLoaderUtils.loadFirstService(MyService.class, classLoader);
if (service != null) {
    service.execute();
`
}

loadFirstService

public static <S> S loadFirstService(Class<S> serviceType, @Nullable ClassLoader classLoader, boolean cached)

Loads the first instance of the specified ServiceLoader service type using the provided ClassLoader, 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 the io.microsphere.lang.Prioritized interface are sorted by priority.

Example Usage

`ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService service = ServiceLoaderUtils.loadFirstService(MyService.class, classLoader, true);
if (service != null) {
    service.initialize();
`
}

loadLastService

public static <S> S loadLastService(Class<S> serviceType)

Loads the last instance of the specified ServiceLoader service 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 the io.microsphere.lang.Prioritized interface are sorted by priority.

Example Usage

`MyService service = ServiceLoaderUtils.loadLastService(MyService.class);
if (service != null) {
    service.execute();
`
}

loadLastService

public static <S> S loadLastService(Class<S> serviceType, boolean cached)

Loads the last instance of the specified ServiceLoader service 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 the io.microsphere.lang.Prioritized interface are sorted by priority.

Example Usage

`MyService service = ServiceLoaderUtils.loadLastService(MyService.class, true);
if (service != null) {
    service.initialize();
`
}

loadLastService

public static <S> S loadLastService(Class<S> serviceType, @Nullable ClassLoader classLoader)

Loads the last instance of the specified ServiceLoader service type using the provided ClassLoader.

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 the io.microsphere.lang.Prioritized interface are sorted by priority.

Example Usage

`ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService service = ServiceLoaderUtils.loadLastService(MyService.class, classLoader);
if (service != null) {
    service.execute();
`
}

loadLastService

public static <S> S loadLastService(Class<S> serviceType, @Nullable ClassLoader classLoader, boolean cached)

Loads the last instance of the specified ServiceLoader service type using the provided ClassLoader, 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 the io.microsphere.lang.Prioritized interface are sorted by priority.

Example Usage

`ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
MyService service = ServiceLoaderUtils.loadLastService(MyService.class, classLoader, true);
if (service != null) {
    service.initialize();
`
}

See Also

  • ServiceLoader

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally