Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting the current App in Play 2.5.2

I am using the DI to access my App in Play 2.5:

import com.google.inject.{Inject, Provider}
import play.api.mvc._
import play.api.Application

class ApplicationController @Inject()(p: Provider[Application]) extends Controller {
  implicit lazy val app = p.get()
  println(app.hashCode()+"") // Random line to use the injected app
}

I am getting the following error :

ProvisionException: Unable to provision, see the following errors:

1) Tried proxying play.api.Application to support a circular dependency, but circular proxies are disabled. ...

I went on the Play Migration guide : https://www.playframework.com/documentation/2.5.x/Migration25#Handling-legacy-components

and did as they say to avoid circular dependency, however I still have that error.

I've also found a variable called disableCircularProxies which I am not currently using (http://google.github.io/guice/api-docs/latest/api-diffs/changes/docdiffs_com.google.inject.html) as I am not sure how to do so and found very little info about it.

Any idea how to solve this circular dependency error ?

Here is the full stack trace :

   Tried proxying play.api.Application to support a circular dependency, but circular proxies are disabled.
  at play.api.DefaultApplication.class(Application.scala:221)
  while locating play.api.DefaultApplication
  while locating play.api.Application
  at myCustomProject.controllers.ApplicationController.<init>(ApplicationController.scala:9)
  while locating myCustomProject.controllers.ApplicationController
    for parameter 1 at router.Routes.<init>(Routes.scala:31)
  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>(HttpRequestHandler.scala:200)
  while locating play.api.http.JavaCompatibleHttpRequestHandler
  while locating play.api.http.HttpRequestHandler
    for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
  at play.api.DefaultApplication.class(Application.scala:221)
  while locating play.api.DefaultApplication
  while locating play.api.Application

Ultimately, I shouldn't be using this Provider[Application] but rather the configuration, environment, ... from the App. It's just that according to the doc, I should be able to have that working.

like image 780
Scipion Avatar asked Apr 28 '16 08:04

Scipion


1 Answers

Java Version:

@Inject
private Provider<Application> application;

private void someMethod {
   ...
   application.get();
}
like image 111
BatteryAcid Avatar answered Nov 02 '22 06:11

BatteryAcid