使用PlayFramework 2.4.2的ProvisionException [英] ProvisionException using PlayFramework 2.4.2

查看:121
本文介绍了使用PlayFramework 2.4.2的ProvisionException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一个项目从Play 2.2.4迁移到2.4.2,但遇到了一个我无法理解和解决的异常.

I am migrating a project from Play 2.2.4 to 2.4.2 and I am getting an exception which I am not able to understand and solve.

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添加了依赖项注入,例如:

It occurs since I added the dependency injection for the WS API like:

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

可能遗漏或做错了什么?

What could be missing or be done wrong?

推荐答案

此行导致Nullpointer: 当Guice实例化Application类时,ws.url("https://..."); ws为null.此外,将request作为控制器字段不是线程安全的.将您的代码更改为以下内容:

This line causes Nullpointer: ws.url("https://..."); ws is null by the time when Guice instantiates Application class. More over, having a request as controller field is not thread safe. Change your code to following:

public class Application extends Controller {

    private WSClient ws;

    @Inject
    public Application(WSClient ws) {

        this.ws = ws;
        WSRequest request = this.ws.url("https://...");

        ...
    }
}

这篇关于使用PlayFramework 2.4.2的ProvisionException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆