Depending on which version of Play you are using, there are steps you can take to make your code "less WET" and/or more loosely coupled:
Versions up to and including Play 2.0
Extend play.GlobalSettings
(if you are not already doing so), and override the onStart
method:
@Override
public void onStart(final Application app) {
// Initialise your ServicesManager class here
}
Next, I would remove the getServices()
method from your parent controller:
public class RestController extends Controller {
protected static UserService userService = ServicesManager.getUserService();
// Other service declarations ...
}
This way you don't have to invoke getServices()
from each of your action methods.
Play 2.1 onwards
As you've mentioned, in earlier versions of Play controller classes are not instantiated and actions are defined as static methods. Play 2.1 introduced the concept of controller instantiation, and gave developers control over managing controller creation. You can find a reference implementation here of how to tell the Spring framework to manage your controllers and wire in service classes. This now contributes to looser-coupled code.