2

I am using Play! framework 1.2.5 for one of my application. Initially I was resolving dependencies either by creating new instance of the class or using factory. But my application grows and it becomes harder to manage dependencies in such way. I'm going to move to dependency injection with Google Guice

Looks like, for every controller, I have to write

requestStaticInjection(MyController.class);

to inject service in controller

@Inject
static MyService mySerivce;

This is frustrating, I don't like that controllers has static methods and can access only static variables. I would like to pass dependencies to constructor of controller and I don't want to declare static fields.

Why methods of play controllers are static ? Is it some kind of limitation ? Is there are other good way to inject classes into Play! controllers

emt14
  • 4,676
  • 7
  • 33
  • 57
user12384512
  • 3,245
  • 10
  • 48
  • 90
  • 1
    Justification for using static methods is can be found here http://stackoverflow.com/questions/5192904/play-framework-uses-a-lot-of-statics – Vineet Bhatia Dec 31 '12 at 11:59

2 Answers2

1

Old question, but I got the same problem and I found my answer here http://typesafe.com/blog/announcing-play-framework-21-the-high-velocit Though, It's not for the same Play version as you are using...

Play 2.1 now supports a new routes syntax that enables calling injected controllers instead of static methods. Simply prefix the controller method in the routes files with an "@" symbol

GET     /                  @controllers.Application.index()

in your conf/routes

jackdbernier
  • 1,392
  • 1
  • 15
  • 31
  • Good to know, that Play are improving. Btw my application grows and now I release that choosing Play was my biggest fault. – user12384512 Sep 15 '13 at 18:13
0

Is there are other good way to inject classes into Play! controllers

No. You should not create a service in a controller - make it a regular class that does not extend Controller. Then call the service class from your controller. Keep your controller code to a minimum - there is a lot of bytecode magic there, so keep controller code simple (lookup object, render results, that sort of thing)

Tom Carchrae
  • 5,916
  • 2
  • 32
  • 33
  • The question was how to call other classes from static controllers. And looks like, there is not so many options, with guava it can be done using requestStaticInjection method – user12384512 Jan 25 '13 at 12:53
  • My point, if not clear enough, was do not do this on Controller classes. It is simple though, just create your service in the app/models/ part of the project and you can use whatever you like there. I am just trying to gently suggest you don't use the wrong end of the hammer to bang in the nail! :) A play controller is *not* a normal Java class - trace through what happens with it sometime - there is a lot of bytecode injection. – Tom Carchrae Jan 25 '13 at 18:09
  • Thanks for you suggestion, but the question was about the right way of injection service into controller class =) – user12384512 Jan 30 '13 at 19:36