Access to the global object in the game! 2.0 with Java

I have expanded the GlobalSettings class, as shown in the Global Application Settings lesson.

How do I access an instance of this class, say, from a view method? I assume that one instance was created at application startup and that it is probably a member of some object.

I would expect, for example, to find it here:

Play.application().getGlobalSettings() 

or something similar, but I could not find it anywhere in the Play hierarchy. *.

Any ideas? Thanks.

+8
source share
2 answers

I am new to Play 2.0, however, I think you better work using the plugin. Check this:

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

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

 @Inject static MyStaticObj obj; 

And everything else is done automatically using the injection framework. No need to worry about global etc.

However, like you, I spent a lot of time figuring out how to use the GlobalSettings object to do this before opening the plugin implementation infrastructure.

I believe that given how Global is implemented (as a class in a standard / unnamed package), it is not possible to refer to it anywhere in the application code. I'm not sure if it was by design or by accident (it seems that Play people think about Scala quite a bit these days ...). Fortunately, the plugin approach seems to be the best way to handle these common global variables.

+5
source share

Just access the Global object directly. For example, do not write:

 public class Global extends GlobalSettings {} 

to write

 object Global extends GlobalSettings {} 

or

 object Global extends Global {} class Global extends GlobalSettings {} 

and how you can reference the Global object anywhere in your code, just write:

 Global.someMethod() 
-3
source share

All Articles