Class ServiceHelper

java.lang.Object
dev.langchain4j.spi.ServiceHelper

public class ServiceHelper extends Object
Utility wrapper around ServiceLoader.load().
  • Method Details

    • loadFactory

      public static <T> T loadFactory(Class<T> clazz)
      Load the service of a given type with the highest priority.

      If several services share the highest priority, the first one found by the ServiceLoader is returned and a warning is logged, because that order depends on the classpath and is not stable.

      Type Parameters:
      T - the type of service
      Parameters:
      clazz - the type of service
      Returns:
      the service with the highest priority, null if none
    • loadFactories

      public static <T> Collection<T> loadFactories(Class<T> clazz)
      Load all the services of a given type, sorted by priority, highest first.

      Use this when every service is used. When only one is used, use loadFactory(Class) instead, which also warns when the choice depends on classpath order.

      Type Parameters:
      T - the type of service
      Parameters:
      clazz - the type of service
      Returns:
      the list of services, empty if none
    • loadFactories

      public static <T> Collection<T> loadFactories(Class<T> clazz, ClassLoader classLoader)
      Load all the services of a given type.

      Utility mechanism around ServiceLoader.load()

      • If classloader is null, will try ServiceLoader.load(clazz)
      • If classloader is not null, will try ServiceLoader.load(clazz, classloader)

      If the above return nothing, will fall back to ServiceLoader.load(clazz, $this class loader$)

      Services are sorted by priority, highest first. Use this when every service is used. When only one is used, use loadFactory(Class) instead, which also warns when the choice depends on classpath order.

      Type Parameters:
      T - the type of service
      Parameters:
      clazz - the type of service
      classLoader - the classloader to use, may be null
      Returns:
      the list of services, empty if none