[Play 2.5.x] Play.application() is deprecated - What alternative?

3,273 views
Skip to first unread message

Thibault Meyer

unread,
Mar 30, 2016, 5:09:08 AM3/30/16
to play-framework
Hi,

It seem that Play.application() is deprecated, so what's the alternative to get the current application instance from a class (not a ontroller, neither a module) ?


By exemple, I need to access current application instance from this class. How I can instantiate it now ?

thanks

/**
 * ApplicationHelper.
 *
 * @version 16.03.30
 * @since 16.03.30
 */

public class ApplicationHelper {


   
/**
     * Handle to the current application instance.
     *
     * @since 16.03.30
     */

   
private Application application;


   
/**
     * Build a basic instance with injected dependency.
     *
     * @param application The current application instance
     * @since 16.03.30
     */

   
@Inject
   
public ApplicationHelper(final Application application) {
       
this.application = application;
   
}


   
/**
     * Get the current application instance.
     *
     * @return The current application
     * @see Application
     * @since 16.03.30
     */

   
public Application getApplication() {
       
return this.application;
   
}
}





shraddha jog

unread,
Apr 18, 2016, 2:40:56 AM4/18/16
to play-framework
I m also having the same problem. Can someone suggest what should we use to get current application instance?

Greg Methvin

unread,
Apr 18, 2016, 3:14:23 AM4/18/16
to play-framework
The recommended way to migrate is detailed in the migration guide.

We recommend moving away from static global state and using dependency injection in your application instead.

If you still need static state when Play.application is removed, you can still use static injection, or override the ApplicationLoader and manually set up and tear down your static state there.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/play-framework/494da14f-f281-42b4-920f-9c4aea5d1ada%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Greg Methvin
Senior Software Engineer

Johan Dahlberg

unread,
Apr 29, 2016, 6:34:09 AM4/29/16
to play-framework
I use Application to access the injector and tried to inject Application into my controller (Java) this way:

@Inject
play.Application application;

but now I get this error instead:

2) Tried proxying play.api.Application to support a circular dependency, but circular proxies are disabled.
  at play.api.DefaultApplication.class(Unknown Source)
  while locating play.api.DefaultApplication
  while locating play.api.Application
    for parameter 0 at play.DefaultApplication.<init>(Unknown Source)
  at play.DefaultApplication.class(Unknown Source)
  while locating play.DefaultApplication
  while locating play.Application
    for field at controllers.ApiV1.application(Unknown Source)
  while locating controllers.ApiV1
    for parameter 3 at router.Routes.<init>(Unknown Source)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router
    for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(Unknown Source)
  while locating play.api.http.JavaCompatibleHttpRequestHandler
  while locating play.api.http.HttpRequestHandler
    for parameter 4 at play.api.DefaultApplication.<init>(Unknown Source)
  at play.api.DefaultApplication.class(Unknown Source)
  while locating play.api.DefaultApplication
  while locating play.api.Application


Or should I maybe replace "application.injector().instanceOf..." with something else?

/Johan

Simon Jacobs

unread,
Apr 29, 2016, 6:39:45 AM4/29/16
to play-framework
Try
    @Inject
    protected Provider<Application> app;

and e.q.:    
String sender  = app.get().configuration().getString(SENDER);

Greg Methvin

unread,
Apr 29, 2016, 6:42:12 AM4/29/16
to play-framework
Hi Johan,

Your application depends on the controllers and your controller depends on the application, hence the circular dependency.

If you are using application.injector().instanceOf[Foo], you probably just want to inject a Foo directly into your controller instead of injecting the whole application.

Rarely should you actually need a reference to the whole application, but if you do you can inject Provider<Application>. I would definitely consider this an anti-pattern though.


For more options, visit https://groups.google.com/d/optout.

Amit Paz

unread,
Dec 27, 2016, 12:56:35 PM12/27/16
to Play Framework
Hello Greg,
I need to get the path of the application - 
Should we still use the deprecated method / inject application via a provider

Greg Methvin

unread,
Dec 27, 2016, 3:08:47 PM12/27/16
to Play Framework
You should be able to inject Environment and call environment.rootPath.

Greg




You received this message because you are subscribed to the Google Groups "Play Framework" group.

To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages