Class Services

java.lang.Object
com.oorian.inject.Services

public final class Services extends Object
Static accessor for the application's ServiceLocator.

Provides a central point for configuring the active service locator and looking up services from anywhere in the application. Configure in Application.initialize().

Setup with built-in registry:


 ServiceRegistry registry = new ServiceRegistry();
 registry.register(UserService.class, new UserServiceImpl());
 registry.register(EmailService.class, new SmtpEmailService());
 Services.setServiceLocator(registry);
 

Setup with Spring:


 // In a Spring @Configuration class or WebApplicationInitializer
 Services.setServiceLocator(new SpringServiceLocator(applicationContext));
 

Usage in pages and components:


 public class DashboardPage extends HtmlPage {
     private final UserService userService = Services.get(UserService.class);

     @Override
     protected void createBody(Body body) {
         List<User> users = userService.getRecentUsers();
         // ...
     }
 }
 
Since:
2.1
Version:
1.0
Author:
Marvin P. Warble Jr.
See Also:
  • Method Details

    • setServiceLocator

      public static void setServiceLocator(ServiceLocator locator)
      Sets the active service locator.

      Call this once during application startup, typically in Application.initialize().

      Parameters:
      locator - the service locator to use
    • getServiceLocator

      public static ServiceLocator getServiceLocator()
      Returns the active service locator.
      Returns:
      the current service locator, or null if none configured
    • get

      public static <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:
      IllegalStateException - if no service locator has been configured
      ServiceNotFoundException - if the service is not registered
    • has

      public static <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 available, false if not or no locator configured