Package io.github.connellite.container
Class ServiceRegistry
java.lang.Object
io.github.connellite.container.ServiceRegistry
Dynamic registry of application services. The global singleton is available via
getInstance(); isolated registries can be created with create().
For an interface type, get(Class) returns a lazy proxy that resolves the current
registration on every method call. The proxy can be obtained before register(Class, Object);
resolution fails at first use with ServiceNotRegisteredException if nothing is registered yet.
A later register makes subsequent invocations succeed; replacing a registration is visible
on the next call (OSGi-style dynamic lookup).
For a concrete class, get looks up the registered instance immediately and throws
ServiceNotRegisteredException when the type is missing.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all registrations and cached lazy proxies in this registry.booleanstatic ServiceRegistrycreate()Creates a new isolated registry that does not share state withgetInstance().<T> TReturns a lazy proxy for an interface type, or the registered instance for a concrete type.static ServiceRegistry<T> voidRegistersinstanceundertype.<T> booleanregisterIfAbsent(Class<T> type, T instance) Registersinstanceonly whentypeis not registered yet.<T> Tunregister(Class<T> type) Removes the registration fortype, if any.
-
Constructor Details
-
ServiceRegistry
public ServiceRegistry()
-
-
Method Details
-
getInstance
-
create
Creates a new isolated registry that does not share state withgetInstance(). -
register
Registersinstanceundertype. Replaces any previous registration for the same type.- Throws:
IllegalArgumentException- ifinstanceis not an instance oftype
-
registerIfAbsent
Registersinstanceonly whentypeis not registered yet.- Returns:
trueif registered,falseiftypewas already present- Throws:
IllegalArgumentException- ifinstanceis not an instance oftype
-
unregister
Removes the registration fortype, if any.- Returns:
- the previous instance, or
nullif none was registered
-
get
Returns a lazy proxy for an interface type, or the registered instance for a concrete type. -
contains
-
clear
public void clear()Removes all registrations and cached lazy proxies in this registry.
-