Service factories

When requesting dependencies, the implementations may differ for various contexts. DiVine lets you specify factories, which have the purpose of creating instances based on the given parameters.

You can bind a factory for a service directly.

@Service(factory = MyFactory.class)
class MyService { ... }

You can define your factory somewhere.

The factory properties may differ depending on your use case.

... and so on.

When requesting a dependency for a service, you must also pass in the properties, as specified by the service.

Note that parameter arguments are checked at runtime, so factory types are guaranteed inside the factory class.

The following code showcases a simple way of requesting different implementations for a service.

Last updated