我正在使用DI在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
}
我收到以下错误:
ProvisionException: Unable to provision, see the following errors:
1) Tried proxying play.api.Application to support a circular dependency, but circular proxies are disabled. ...
我继续了Play迁移指南:https://www.playframework.com/documentation/2.5.x/Migration25#Handling-legacy-components
并按照他们的说法做了以避免循环依赖,但我仍然有这个错误。
我还发现了一个名为disableCircularProxies的变量,我目前没有使用它(http://google.github.io/guice/api-docs/latest/api-diffs/changes/docdiffs_com.google.inject.html),因为我不知道该怎么做,并且发现它的信息很少。
知道如何解决这个循环依赖错误吗?
这是完整的堆栈跟踪:
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
最终,我不应该使用此Provider [Application],而应该使用App中的配置,环境.... 根据文档,我应该能够做到这一点。
答案 0 :(得分:4)
Java版本:
@Inject
private Provider<Application> application;
private void someMethod {
...
application.get();
}