Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the Global object in play! 2.0 with Java

I have extended the GlobalSettings class as shown in the tutorial Application global settings.

How do I then gain access to the instance of this class, let's say from a view method? I'm assuming that one instance was created when the application was started and that it is probably a member of some object.

I would have expected for example to find it here:

Play.application().getGlobalSettings()

or something similar, but I couldn't find it anywhere in the Play.* hierarchy.

Any ideas? Thanks.

like image 404
Nitzan Tomer Avatar asked Apr 19 '12 22:04

Nitzan Tomer


1 Answers

I'm new to Play 2.0, however, I think you're better served by using plugin injection. Check this out:

https://github.com/typesafehub/play-plugins/tree/master/inject

Using this approach you simply add the following line to your controller (and some other configuration, as documented in the link above):

@Inject static MyStaticObj obj;

And all the rest is done automatically using the injection framework. No need to worry about global, etc.

That said, like you I spent a lot of time trying to figure out how to use the GlobalSettings object for this before discovering the plugin injection framework.

My sense is that given how Global is implemented (as class in the default/unnamed package) it's not possible to reference it anywhere in the application code. I'm not sure if this was by design or by accident (it seems that the Play folks are thinking about Scala quite a bit these days...). Fortunately the plugin approach seems to be better way to handle these shared globals.

like image 176
kpw Avatar answered Nov 13 '22 01:11

kpw