Package com.zetaplugins.zetacore.di
Class ServiceRegistry
java.lang.Object
com.zetaplugins.zetacore.di.ServiceRegistry
A registry for managing and injecting service instances.
-
Nested Class Summary
Nested Classes -
Constructor Summary
ConstructorsConstructorDescriptionServiceRegistry(JavaPlugin plugin) Creates a new ServiceRegistry for the given plugin.ServiceRegistry(JavaPlugin plugin, boolean requireServiceAnnotation, String packagePrefix) Creates a new ServiceRegistry for the given plugin. -
Method Summary
Modifier and TypeMethodDescription<T> TgetOrCreate(Class<T> cls) Gets an existing instance of the specified class, or creates one if it doesn't exist.voidInitializes and registers all eagerly loaded singleton services found in the specified package.voidinjectServices(Object target) Injects service instances into the fields of the target object.voidregisterInstance(Class<?> cls, Object instance) Registers an existing instance in the registry under a specific class.voidregisterInstance(Object instance) Registers an existing instance in the registry.
-
Constructor Details
-
ServiceRegistry
Creates a new ServiceRegistry for the given plugin. Doesn't require theServiceannotation on managed classes.- Parameters:
plugin- The main plugin instance.
-
ServiceRegistry
Creates a new ServiceRegistry for the given plugin.- Parameters:
plugin- The main plugin instance.requireServiceAnnotation- Whether to require theServiceannotation on managed classes.
-
-
Method Details
-
initializeEagerServices
public void initializeEagerServices()Initializes and registers all eagerly loaded singleton services found in the specified package. -
registerInstance
Registers an existing instance in the registry.- Parameters:
instance- The instance to register.
-
registerInstance
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
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
Injects service instances into the fields of the target object.- Parameters:
target- The target object to inject services into.
-