Class ServiceHelper
java.lang.Object
dev.langchain4j.spi.ServiceHelper
Utility wrapper around
ServiceLoader.load().-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Collection<T> loadFactories(Class<T> clazz) Load all the services of a given type, sorted bypriority, highest first.static <T> Collection<T> loadFactories(Class<T> clazz, ClassLoader classLoader) Load all the services of a given type.static <T> TloadFactory(Class<T> clazz) Load the service of a given type with the highestpriority.
-
Method Details
-
loadFactory
Load the service of a given type with the highestpriority.If several services share the highest priority, the first one found by the
ServiceLoaderis 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
Load all the services of a given type, sorted bypriority, 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
Load all the services of a given type.Utility mechanism around
ServiceLoader.load()- If classloader is
null, will tryServiceLoader.load(clazz) - If classloader is not
null, will tryServiceLoader.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, useloadFactory(Class)instead, which also warns when the choice depends on classpath order.- Type Parameters:
T- the type of service- Parameters:
clazz- the type of serviceclassLoader- the classloader to use, may be null- Returns:
- the list of services, empty if none
- If classloader is
-