ServiceManager Three Mintute Intro

Configuring POJOs in the Framework Using Code

A simple way of configuring the framework is to do so in code. Using it this way, you simply add services to ServiceManager in an initialization class. For a web application, it's great to do this in a servlet where you configure the framework in the servlet's init() method and configure the sevlet to load-on-startup.

Programatic Configuration

A simple example of configuring the framework is as follows:

// Use the 'default' instance of ServiceManager
ServiceManager services = ServiceManager.getInstance();

// Use convenience constructor to create a service
// whose implementation is a plain Java object.
services.register(new Service(GreetingInterface.class, Greeting.class));

// A service implementation that is a POJO can
// also be created by using the ClassLocator.
Locator locator = new ClassLocator(DateProvider.class.getName());
Service service = new Service(DateProviderInterface.class, locator);
services.register(service);

Both of these registrations associate a plain Java object with the specified interface. The Service class has a constructor which can associate POJO implementations with interfaces quite easily. The second example shows creating a POJO locator, associating it with a service (and thus an interface) and finally registering that service.

Using Services

Setup

Assume you have an interface and a class like:

public interface GreetingInterface {
  public String getGreeting();
}

public class Greeting implements GreetingInterface {
  public String getGreeting() {
    return "Hello user!";
  }
}

You register the service using the Service convenience constructor:

ServiceManager services = ServiceManager.getInstance();
services.register(new Service(GreetingInterface.class, Greeting.class));

This can be done in an initialization class. As long as your application uses the same class loader to load ServiceManager and your other classes, ServiceManager will retain it's registry information using a singleton-like pattern (but see the note on 'applications' in documentation).

Getting and Calling a Service

You can later get an instance of the class implementing GreetingInterface like:

GreetingInterface greeting = (GreetingInterface)
  services.serviceInstance(GreetingInterface.class);
System.out.println(greeting.getGreeting());
System.out.println("You are now logged in.");

Property File Configuration

Finally you can configuration ServiceManager and associate POJO implementations with interfaces using a properties file. The name of the properties file should be servicemanager.properties if you get an instance of ServiceManager using the static ServiceManager.getInstance() method (with no parameters).

The key in the property file is the fully qualified name of the interface.

The value in the property file is the fully qualified name of the implementation.

For example, if your interface and implemenation are in the example.services package:

example.services.GreetingInterface=example.services.Greeting

would register a Service just the same as the code used above.

services.register(
  new Service(example.services.GreetingInterface.class, 
    example.services.Greeting.class));

Example Code

Download an example to show you this and more.