Services are responsible for the application logic that does not belong to a single controller. They are meant to be treated as singletons, injected to MVC members by following a naming convention. Services are optional artifacts, and as such there is no default folder created for them when a new application is created.Services must be located inside the griffon-app/services
directory with a Service
suffix. The create-service command performs this job for you; also adding a unit test for the given service.Let's say you need to create a Math service, the command to invoke would begriffon create-service math
This results in the following files being created
griffon-app/services/MathService.groovy
- the service class.
test/unit/MathServiceTests.groovy
- service unit test.
A trivial implementation of an addition operation performed by the MathService would look like the following snippetclass MathService {
def addition(a, b) { a + b }
}
Using this service from a Controller is a straight forward task. As mentioned earlier services will be injected by name, which means you only need to define a property whose name matches the uncapitalized service name, for exampleclass MyController {
def mathService def action = {
model.result = mathService.addition model.a, model.b
}
}
The type of the service class is optional as basic injection is done by name alone.
Service injection is trivial, it does not provide a full blown DI, in other words further service dependencies will not be resolved. You will need a richer DI solution in order to achieve this, fortunately there is a Spring plugin that does this and more.
Given that services are inherently treated as singletons they are also automatically registered as application event listeners. Be aware that services will be instantiated lazily which means that some events might not reach a particular service if it has not been instantiated by the framework by the time of event publication. It also discouraged to use the @Singleton annotation on a Service class as it will cause trouble with the automatic singleton management Griffon has in place.Lastly, all services instances will become available through an instance of type griffon.core.ServiceManager
. This helper class exposes available services via a Map. You can query all currently available services in the following mannerapp.serviceManager.services.each { name, instance ->
// do something cool with services
}
You can also query for a particular service instance in the following waydef fooService = app.serviceManager.findService('foo')
It's worth mentioning that the previous method will instantiate the service if it wasn't available up to that point.All services are instantiated lazily by default. You can change this behavior by adding a configuration flag to Config.groovy
griffon.services.eager.instantiation = true