ProvisionException using PlayFramework 2.4.2

末鹿安然 提交于 2019-12-10 17:01:49

问题


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

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://...");

    ...
}

The build.sbt file includes the necessary configuration

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?


回答1:


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://...");

        ...
    }
}


来源:https://stackoverflow.com/questions/31610945/provisionexception-using-playframework-2-4-2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!