从Play 2.7.x起,我无法获得play.api.Application
,我尝试了多种方法,例如从自定义模块(插件)甚至从我的ApplicationController
。我总是收到以下循环依赖错误:
ProvisionException: Unable to provision, see the following errors:
1) Found a circular dependency involving play.api.Application, and circular dependencies are disabled.
at play.api.DefaultApplication.class(Application.scala:252)
while locating play.api.DefaultApplication
while locating play.api.Application
for the 10th parameter of controllers.ApplicationController.<init>(ApplicationController.scala:30)
while locating controllers.ApplicationController
for the 2nd parameter of router.Routes.<init>(Routes.scala:111)
while locating router.Routes
while locating play.api.inject.RoutesProvider
while locating play.api.routing.Router
for the 3rd parameter of play.api.http.DefaultHttpRequestHandler.<init>(HttpRequestHandler.scala:120)
while locating play.api.http.DefaultHttpRequestHandler
while locating play.api.http.HttpRequestHandler
for the 6th parameter of play.api.DefaultApplication.<init>(Application.scala:253)
at play.api.DefaultApplication.class(Application.scala:252)
while locating play.api.DefaultApplication
while locating play.api.Application
1 error
例如,从我的自定义模块DynamicTemplateLoader
:
@Singleton
class DynamicTemplateLoader @Inject(app: play.api.Application) {
...
}
或来自我的ApplicationController
:
class ApplicationController @Inject() (
components: ControllerComponents
)(
implicit app: play.api.Application
) extends AbstractController(components) with I18nSupport {
...
}
为什么我需要play.api.Application
?我需要掌握app.classloader
才能使用插件的反射。
通过从注入play.api.Application
实例改为注入javax.inject.Provider[play.api.Application]
实例解决了该问题。