Class ServiceLoader

java.lang.Object
org.silverpeas.kernel.util.ServiceLoader

public class ServiceLoader extends Object
Service loader which takes into account of the Priority annotation to load the correct single implementation of a service interface when several implementations are available. If several implementations are available for a given service and no ones are annotated with Priority, then the first found one is loaded. This service loader provides also the possibility to kept in cache the instance of the loaded implementation so that is can be get without having to load it again.
Author:
mmoquillon
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> T
    get(Class<T> service)
    Gets an instance of the specified service if already loaded, otherwise loads it by the Java SPI API and caches it so that it can be returned later without loading it again.
    static <T> T
    load(Class<T> service)
    Loads an instance of the specified service by using the Java SPI API.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • load

      public static <T> T load(Class<T> service)
      Loads an instance of the specified service by using the Java SPI API. If there are several implementations of the service available in the classpath, then the one with higher priority is got, otherwise this is the first one found. The priority is given with the Priority annotation.
      Type Parameters:
      T - the type of the service.
      Parameters:
      service - the service to load.
      Returns:
      an instance of the loaded service.
    • get

      public static <T> T get(Class<T> service)
      Gets an instance of the specified service if already loaded, otherwise loads it by the Java SPI API and caches it so that it can be returned later without loading it again.
      Type Parameters:
      T - the type of the service.
      Parameters:
      service - the service to get.
      Returns:
      an instance of the service.
      See Also:
      Implementation Requirements:
      the application cache is used to maintain the instance in memory all along the application is running.