Class ServiceDescriptor

java.lang.Object
com.github.alex1304.rdi.config.ServiceDescriptor

public class ServiceDescriptor
extends Object
Contains all the metadata on how to instantiate a service and how to inject all the dependencies it needs.

This class implements equals(Object) and hashCode() on the service reference, meaning that there should be only one descriptor per service container and per service.

See Also:
builder(ServiceReference)
  • Method Details

    • getServiceReference

      public ServiceReference<?> getServiceReference()
      Gets the service reference targeted by this descriptor.
      Returns:
      the service reference
    • isSingleton

      public boolean isSingleton()
      Gets whether the service is configured as singleton.
      Returns:
      a boolean
    • getFactoryMethod

      public FactoryMethod getFactoryMethod()
      Gets information about the factory method of the service that will be subject to dependency injection.
      Returns:
      the factory method
    • getSetterMethods

      public List<SetterMethod> getSetterMethods()
      Gets information about all the setter methods of the service that will be subject to dependency injection.
      Returns:
      the setter methods
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals​(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • standalone

      public static ServiceDescriptor standalone​(ServiceReference<?> serviceReference, boolean isSingleton)
      Convenience method to create a descriptor for a service that does not have any dependencies. The class must define a public no-arg constructor for it to work. This is equivalent to:
       ServiceDescriptor.builder(serviceReference).setSingleton(isSingleton).build();
       

      This variant allows to specify whether the service should be a singleton. If you want to define dependencies for the service, see builder(ServiceReference).

      Parameters:
      serviceReference - the service reference
      isSingleton - whether the service should be instantiated only once or if a new instance should be created every time it is requested
      Returns:
      a new ServiceDescriptor
      Throws:
      RdiException - if the class does not have a public no-arg constructor
    • standalone

      public static ServiceDescriptor standalone​(ServiceReference<?> serviceReference)
      Convenience method to create a descriptor for a service that does not have any dependencies. The class must define a public no-arg constructor for it to work. This is equivalent to:
       ServiceDescriptor.builder(serviceReference).build();
       

      This variant will configure the service as a singleton by default. If you do not want it to be singeton, you may use the overload standalone(ServiceReference, boolean) instead. If you want to define dependencies for the service, see builder(ServiceReference).

      Parameters:
      serviceReference - the service reference
      Returns:
      a new ServiceDescriptor
      Throws:
      RdiException - if the class does not have a public no-arg constructor
    • builder

      public static ServiceDescriptor.Builder builder​(ServiceReference<?> ref)
      Creates a new builder for a ServiceDescriptor.

      The builder will allow you to define all the dependencies for the service, in factory methods as well as in setters. If your service does not require any dependency, you may prefer to use standalone(ServiceReference) and standalone(ServiceReference, boolean).

      Parameters:
      ref - the service reference targeted by the descriptor to build
      Returns:
      a new builder