Package com.oorian.inject
Class Services
java.lang.Object
com.oorian.inject.Services
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 Summary
Modifier and TypeMethodDescriptionstatic <T> TResolves a service by its type.static ServiceLocatorReturns the active service locator.static <T> booleanChecks whether a service is available for the given type.static voidsetServiceLocator(ServiceLocator locator) Sets the active service locator.
-
Method Details
-
setServiceLocator
Sets the active service locator.Call this once during application startup, typically in
Application.initialize().- Parameters:
locator- the service locator to use
-
getServiceLocator
Returns the active service locator.- Returns:
- the current service locator, or
nullif none configured
-
get
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 configuredServiceNotFoundException- if the service is not registered
-
has
Checks whether a service is available for the given type.- Type Parameters:
T- the service type- Parameters:
serviceClass- the service class or interface- Returns:
trueif a service is available,falseif not or no locator configured
-