Class ServiceRegistry

java.lang.Object
com.oorian.inject.ServiceRegistry
All Implemented Interfaces:
ServiceLocator

public class ServiceRegistry extends Object implements ServiceLocator
Lightweight, built-in service container for projects not using Spring or CDI.

Supports two registration modes:

  • Singleton — register a pre-created instance, returned on every lookup
  • Factory — register a Supplier, called on each lookup to create a new instance

Usage:


 // In Application.initialize()
 ServiceRegistry registry = new ServiceRegistry();

 // Singleton registration
 registry.register(UserService.class, new UserServiceImpl(dataSource));
 registry.register(EmailService.class, new SmtpEmailService(config));

 // Factory registration (new instance per lookup)
 registry.registerFactory(ReportGenerator.class, () -> new ReportGenerator());

 // Activate as the service locator
 Services.setServiceLocator(registry);
 

Thread-safe. All operations use ConcurrentHashMap.

Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Constructor Details

    • ServiceRegistry

      public ServiceRegistry()
      Creates an empty service registry.
  • Method Details

    • register

      public <T> ServiceRegistry register(Class<T> serviceClass, T instance)
      Registers a singleton service instance.

      The same instance is returned for every get(Class) call.

      Type Parameters:
      T - the service type
      Parameters:
      serviceClass - the service class or interface
      instance - the service instance
      Returns:
      this registry for method chaining
    • registerFactory

      public <T> ServiceRegistry registerFactory(Class<T> serviceClass, Supplier<T> factory)
      Registers a factory that creates a new instance on each lookup.
      Type Parameters:
      T - the service type
      Parameters:
      serviceClass - the service class or interface
      factory - the supplier that creates service instances
      Returns:
      this registry for method chaining
    • unregister

      public ServiceRegistry unregister(Class<?> serviceClass)
      Removes a service registration.
      Parameters:
      serviceClass - the service class to unregister
      Returns:
      this registry for method chaining
    • clear

      public ServiceRegistry clear()
      Removes all service registrations.
      Returns:
      this registry for method chaining
    • get

      public <T> T get(Class<T> serviceClass)
      Description copied from interface: ServiceLocator
      Resolves a service by its type.
      Specified by:
      get in interface ServiceLocator
      Type Parameters:
      T - the service type
      Parameters:
      serviceClass - the service class or interface
      Returns:
      the service instance
    • has

      public <T> boolean has(Class<T> serviceClass)
      Description copied from interface: ServiceLocator
      Checks whether a service is available for the given type.
      Specified by:
      has in interface ServiceLocator
      Type Parameters:
      T - the service type
      Parameters:
      serviceClass - the service class or interface
      Returns:
      true if a service is registered for the type