Package dev.webfx.platform.service
Class SingleServiceProvider
java.lang.Object
dev.webfx.platform.service.SingleServiceProvider
Simple utility class to get a provider that is meant to be unique. Its main benefit is that it keeps that provider
in memory, so it will always return the same provider instance, without instantiating it again on subsequent calls.
This would not be the case with the standard ServiceLoader, which creates a new provider instance on each call.
So this utility class can be useful for pieces of code that need to refer to a provider several times, with
the guarantee to have the same unique instance each time. By using this utility class, you spare the declaration
of a field for storing your provider, and it can therefore be used even in interface default methods.
Typical usage:
private static MyProvider getMyProvider() {
return SingleServiceProvider.getProvider(MyProvider.class, () -> ServiceLoader.load(MyProvider.class));
}
Note: the reason why ServiceLoader.load() is written in the caller code and not in this utility class is to be
compliant with the Java Platform Module System. The caller module has to declare in module-info.java that it's using
this service, and only the caller code can ask the ServiceLoader to load this service, this utility class can't.
- Author:
- Bruno Salmon
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <S> S
getProvider
(Class<S> serviceClass) static <S> S
getProvider
(Class<S> serviceClass, Supplier<ServiceLoader<S>> serviceLoaderSupplier) static <S> S
getProvider
(Class<S> serviceClass, Supplier<ServiceLoader<S>> serviceLoaderSupplier, SingleServiceProvider.NotFoundPolicy notFoundPolicy) static <S> void
registerServiceInterceptor
(Class<S> serviceClass, Function<S, S> interceptor) static <S> void
registerServiceProvider
(Class<S> serviceClass, S serviceInstance) static <S> void
registerServiceSupplier
(Class<S> serviceClass, Supplier<ServiceLoader<S>> serviceLoaderSupplier) static <S> void
registerServiceSupplier
(Class<S> serviceClass, Supplier<ServiceLoader<S>> serviceLoaderSupplier, SingleServiceProvider.NotFoundPolicy notFoundPolicy)
-
Constructor Details
-
SingleServiceProvider
public SingleServiceProvider()
-
-
Method Details
-
registerServiceSupplier
public static <S> void registerServiceSupplier(Class<S> serviceClass, Supplier<ServiceLoader<S>> serviceLoaderSupplier) -
registerServiceSupplier
public static <S> void registerServiceSupplier(Class<S> serviceClass, Supplier<ServiceLoader<S>> serviceLoaderSupplier, SingleServiceProvider.NotFoundPolicy notFoundPolicy) -
registerServiceProvider
-
registerServiceInterceptor
-
getProvider
-
getProvider
public static <S> S getProvider(Class<S> serviceClass, Supplier<ServiceLoader<S>> serviceLoaderSupplier) -
getProvider
public static <S> S getProvider(Class<S> serviceClass, Supplier<ServiceLoader<S>> serviceLoaderSupplier, SingleServiceProvider.NotFoundPolicy notFoundPolicy)
-