Scala ProvisionException:类必须有一个(并且只有一个)用@Inject注释的构造函数或一个零参数
我正在PlayFramework中使用Guice,但遇到运行时错误:Scala ProvisionException:类必须有一个(并且只有一个)用@Inject注释的构造函数或一个零参数,scala,playframework,guice,playframework-2.4,Scala,Playframework,Guice,Playframework 2.4,我正在PlayFramework中使用Guice,但遇到运行时错误: Caused by: com.google.inject.ProvisionException: Unable to provision, see the following errors: 1) Could not find a suitable constructor in controllers.DirectUserController. Classes must have either one (and only o
Caused by: com.google.inject.ProvisionException: Unable to provision, see the following errors:
1) Could not find a suitable constructor in controllers.DirectUserController. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at controllers.DirectUserController.class(DirectUserController.scala:90)
while locating com.google.inject.Provider<controllers.DirectUserController>
for parameter 7 at router.Routes.<init>(Routes.scala:124)
while locating router.Routes
while locating play.api.inject.RoutesProvider
while locating play.api.routing.Router
1 error
at com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1025) ~[guice-4.0.jar:na]
at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051) ~[guice-4.0.jar:na]
at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:321) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:316) ~[play_2.11-2.4.3.jar:2.4.3]
at play.api.Application$class.routes(Application.scala:111) ~[play_2.11-2.4.3.jar:2.4.3]
这是我的控制器:
class DirectUserController(var factory:FactoryHandlerTrait) extends Controller {
//rest of the code
}
在build.sbt
中,我添加了:
routesGenerator := InjectedRoutesGenerator
我错过了什么或做错了什么?如何避免这种例外
我使用的是Play 2.4.3和Guice 3.0。正如Guice所说,构造函数必须具有零参数,或者需要使用
@Inject
对其进行注释。对于您的类FactoryHandler
,这些要求都不满足
在解决这个问题时,您可能需要考虑参数s
是否实际上是一个可以由guice注入的参数(可能不是)。因此,您需要以其他方式传递它。如果它只在运行时可用,那么您可能想看看辅助注入。我在前面的回答中已经描述了这是如何工作的:
routesGenerator := InjectedRoutesGenerator