Class ServiceRegistry

java.lang.Object
com.zetaplugins.zetacore.di.ServiceRegistry

public class ServiceRegistry extends Object
A registry for managing and injecting service instances.
  • Constructor Details

    • ServiceRegistry

      public ServiceRegistry(JavaPlugin plugin)
      Creates a new ServiceRegistry for the given plugin. Doesn't require the Service annotation on managed classes.
      Parameters:
      plugin - The main plugin instance.
    • ServiceRegistry

      public ServiceRegistry(JavaPlugin plugin, boolean requireServiceAnnotation, String packagePrefix)
      Creates a new ServiceRegistry for the given plugin.
      Parameters:
      plugin - The main plugin instance.
      requireServiceAnnotation - Whether to require the Service annotation on managed classes.
  • Method Details

    • initializeEagerServices

      public void initializeEagerServices()
      Initializes and registers all eagerly loaded singleton services found in the specified package.
    • registerInstance

      public void registerInstance(Object instance)
      Registers an existing instance in the registry.
      Parameters:
      instance - The instance to register.
    • registerInstance

      public void registerInstance(Class<?> cls, Object instance)
      Registers an existing instance in the registry under a specific class.
      Parameters:
      cls - The class to register the instance under.
      instance - The instance to register.
    • getOrCreate

      public <T> T getOrCreate(Class<T> cls)
      Gets an existing instance of the specified class, or creates one if it doesn't exist.
      Type Parameters:
      T - The type of the instance.
      Parameters:
      cls - The class of the instance to get or create.
      Returns:
      The existing or newly created instance.
    • injectServices

      public void injectServices(Object target)
      Injects service instances into the fields of the target object.
      Parameters:
      target - The target object to inject services into.