[2.3.8-java] Spring injection vs static actions

205 views
Skip to first unread message

jake k.

unread,
Mar 5, 2015, 10:38:19 AM3/5/15
to play-fr...@googlegroups.com
Hi All,

I am quite new in play framework. I am trying to inject spring bean to my controller:

@Component
public class Application extends Controller {
private static RepositoryUser ru;
@Inject
public Application(RepositoryUser ru) {
this.ru = ru;
}
public static Result index() {
return ok(index.render(""));
}
}


it works fine when action is declared in static way. However I've seen some posts that it is not required any more. Unfortunately I'm getting compilation errors when I'm removig 'static'.

Ideally I would like to inject bean via setters not via constructor and I would like to avoid doing it in static way.

Thanks,
Jakub

raunak

unread,
Mar 5, 2015, 10:44:11 AM3/5/15
to play-fr...@googlegroups.com
What compilation errors are you seeing?

I do the following in order to have my methods declared in a non-static way. 

1. Declare routesGenerator := InjectedRoutesGenerator in build.sbt
2. Either declare your controller as singleton (using @singleton annotation) or make use of @ annotation - James explains the difference in an earlier question I asked, should also apply to 2.3.8 - https://groups.google.com/forum/#!topic/play-framework/LLxd9rg-SRg
3. Remove static keywords from your method.
4. Run your code from console (i've found this to be a good idea rather than running it from the IDE).   

jake

unread,
Mar 5, 2015, 11:19:32 AM3/5/15
to play-fr...@googlegroups.com
Hi raunak,

Thanks for your quick answer. Does routesGenerator := InjectedRoutesGenerator actually works for java?

Thanks,
Jake.

raunak

unread,
Mar 5, 2015, 11:31:20 AM3/5/15
to play-fr...@googlegroups.com
Yes it does.

I'll try to post an example on Github later today. 

jake

unread,
Mar 5, 2015, 11:36:42 AM3/5/15
to play-fr...@googlegroups.com
Thanks raunak, I will keep searching by then.

jake

unread,
Mar 5, 2015, 12:18:20 PM3/5/15
to play-fr...@googlegroups.com
Hi raunak,
I've tried building templates from git using activator in version 1.3.2 on both windows and linux machines I am getting compilation errors:

build.sbt:25: error: not found: value routesGenerator
routesGenerator := InjectedRoutesGenerator

Do you have a clue?


On Thursday, 5 March 2015 16:31:20 UTC, raunak wrote:

raunak

unread,
Mar 5, 2015, 12:38:41 PM3/5/15
to play-fr...@googlegroups.com
My apologies, but InjectedRoutesGenerator does not exist in Play 2.3.x. 

To do DI in 2.3.x based on Spring, here's an example - https://github.com/guillaumebort/play20-spring-demo

Hope this helps. 

jake

unread,
Mar 6, 2015, 7:00:20 AM3/6/15
to play-fr...@googlegroups.com
In fact that example is quite old but it gave me some ideas. I have created and example in a github how to bring all together with spring4 mongodb and play 2.3.8

jake

unread,
Mar 6, 2015, 7:00:37 AM3/6/15
to play-fr...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages