Interface ServiceLocator

All Known Implementing Classes:
ServiceRegistry

public interface ServiceLocator
Abstracts a dependency injection container for resolving services.

Implementations bridge Oorian to an external DI framework (Spring, CDI) or use the built-in ServiceRegistry. The active locator is configured via Services.setServiceLocator(ServiceLocator).

Built-in implementations:

Custom implementations (external modules):


 // Spring integration
 public class SpringServiceLocator implements ServiceLocator {
     private final ApplicationContext context;

     public SpringServiceLocator(ApplicationContext context) {
         this.context = context;
     }

     public <T> T get(Class<T> serviceClass) {
         return context.getBean(serviceClass);
     }

     public <T> boolean has(Class<T> serviceClass) {
         return context.getBeanNamesForType(serviceClass).length > 0;
     }
 }
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    get(Class<T> serviceClass)
    Resolves a service by its type.
    <T> boolean
    has(Class<T> serviceClass)
    Checks whether a service is available for the given type.
  • Method Details

    • get

      <T> T get(Class<T> serviceClass)
      Resolves a service by its type.
      Type Parameters:
      T - the service type
      Parameters:
      serviceClass - the service class or interface
      Returns:
      the service instance
      Throws:
      ServiceNotFoundException - if no service is registered for the type
    • has

      <T> boolean has(Class<T> serviceClass)
      Checks whether a service is available for the given type.
      Type Parameters:
      T - the service type
      Parameters:
      serviceClass - the service class or interface
      Returns:
      true if a service is registered for the type