ProvisionException 使用 PlayFramework 2.4.2
ProvisionException using PlayFramework 2.4.2
我正在将一个项目从 Play 2.2.4 迁移到 2.4.2,我遇到了一个我无法理解和解决的异常。
Unexpected exception
ProvisionException: Unable to provision, see the following errors:
1) Error injecting constructor, java.lang.NullPointerException
at controllers.Application.<init>(Application.java:33)
while locating controllers.Application
for parameter 1 at router.Routes.<init>(Routes.scala:36)
while locating router.Routes
while locating play.api.inject.RoutesProvider
while locating play.api.routing.Router
1 error
这是因为我为 WS API 添加了依赖注入,例如:
public class Application extends Controller {
@Inject
WSClient ws;
WSRequest request = ws.url("https://...");
...
}
build.sbt
文件包含必要的配置
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs
)
// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator
什么可能遗漏或做错了什么?
此行导致空指针:
ws.url("https://...");
ws 在 Guice 实例化 Application
class 时为空。此外,将 request
作为控制器字段不是线程安全的。将您的代码更改为以下内容:
public class Application extends Controller {
private WSClient ws;
@Inject
public Application(WSClient ws) {
this.ws = ws;
WSRequest request = this.ws.url("https://...");
...
}
}
我正在将一个项目从 Play 2.2.4 迁移到 2.4.2,我遇到了一个我无法理解和解决的异常。
Unexpected exception
ProvisionException: Unable to provision, see the following errors:
1) Error injecting constructor, java.lang.NullPointerException
at controllers.Application.<init>(Application.java:33)
while locating controllers.Application
for parameter 1 at router.Routes.<init>(Routes.scala:36)
while locating router.Routes
while locating play.api.inject.RoutesProvider
while locating play.api.routing.Router
1 error
这是因为我为 WS API 添加了依赖注入,例如:
public class Application extends Controller {
@Inject
WSClient ws;
WSRequest request = ws.url("https://...");
...
}
build.sbt
文件包含必要的配置
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs
)
// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator
什么可能遗漏或做错了什么?
此行导致空指针:
ws.url("https://...");
ws 在 Guice 实例化 Application
class 时为空。此外,将 request
作为控制器字段不是线程安全的。将您的代码更改为以下内容:
public class Application extends Controller {
private WSClient ws;
@Inject
public Application(WSClient ws) {
this.ws = ws;
WSRequest request = this.ws.url("https://...");
...
}
}