CreationException:无法使用com.google.inject.AbstractModule在Play 2.5.18中创建注入器错误以替换GlobalSettings Java代码

时间:2018-04-03 20:40:23

标签: java dependency-injection playframework

我正在将Play应用程序从2.5.12升级到2.5.18,当我启动应用程序时(使用sbt),我收到此错误:

CreationException:无法创建注入器,请参阅以下错误:

1) Error injecting constructor, java.lang.NullPointerException
  at modules.OnStart.<init>(OnStart.java:15)
  at modules.Global.configure(Global.java:9) (via modules: com.google.inject.util.Modules$OverrideModule -> modules.Global)
  while locating modules.OnStart

1 error

其余的错误是:

No source available, here is the exception stack trace:
->com.google.inject.CreationException: Unable to create injector, see the following errors:

1) Error injecting constructor, java.lang.NullPointerException
  at modules.OnStart.<init>(OnStart.java:15)
  at modules.Global.configure(Global.java:9) (via modules: com.google.inject.util.Modules$OverrideModule -> modules.Global)
  while locating modules.OnStart

1 error
     com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:470)
     com.google.inject.internal.InternalInjectorCreator.injectDynamically(InternalInjectorCreator.java:184)
     com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:110)
     com.google.inject.Guice.createInjector(Guice.java:99)
     com.google.inject.Guice.createInjector(Guice.java:84)
     play.api.inject.guice.GuiceBuilder.injector(GuiceInjectorBuilder.scala:181)
     play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:137)
     play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
     play.core.server.DevServerStart$$anon$1.$anonfun$get$6(DevServerStart.scala:171)
     play.utils.Threads$.withContextClassLoader(Threads.scala:21)
     play.core.server.DevServerStart$$anon$1.$anonfun$get$3(DevServerStart.scala:168)
     scala.Option.map(Option.scala:146)
     play.core.server.DevServerStart$$anon$1.$anonfun$get$2(DevServerStart.scala:133)
     scala.util.Success.flatMap(Try.scala:247)
     play.core.server.DevServerStart$$anon$1.$anonfun$get$1(DevServerStart.scala:131)
     scala.concurrent.Future$.$anonfun$apply$1(Future.scala:655)
     scala.util.Success.$anonfun$map$1(Try.scala:251)
     scala.util.Success.map(Try.scala:209)
     scala.concurrent.Future.$anonfun$map$1(Future.scala:289)
     scala.concurrent.impl.Promise.liftedTree1$1(Promise.scala:29)
     scala.concurrent.impl.Promise.$anonfun$transform$1(Promise.scala:29)
     scala.concurrent.impl.CallbackRunnable.run(Promise.scala:60)
     scala.concurrent.impl.ExecutionContextImpl$AdaptedForkJoinTask.exec(ExecutionContextImpl.scala:140)
     java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
     java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
     java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
     java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)

我根据这篇文章创建了新的模块来替换弃用的GlobalSettings

https://www.playframework.com/documentation/2.5.x/GlobalSettings

以下是错误中提到的类:

Global.java

package modules;

import com.google.inject.AbstractModule;

public class Global extends AbstractModule {

    @Override
    public void configure() {
        bind(OnStart.class).asEagerSingleton();
        bind(OnStop.class).asEagerSingleton();
    }

}

OnStart.java

package modules;

import javax.inject.Inject;
import javax.inject.Singleton;

import jobs.JobControl;
import com.typesafe.config.Config;
import play.Logger;

@Singleton
public class OnStart {

    @Inject
    public OnStart(Config config) {
        Logger.debug("Application started...");     
        // Turn on scheduler?
        String schedulerEnabled;
        String dueDateRun;
        schedulerEnabled = config.getString("scheduler.enabled");
        dueDateRun = config.getString("duedate.run.flag");
        if (schedulerEnabled.equals("true")) {
            //JobControl.cleanupTables();
            //JobControl.emptyRecycleBin();
            if (dueDateRun.equals("on")) {
                JobControl.dueDateNotifications();
            }
        }
    }
}

我不知道该怎么做才能解决这个问题。

1 个答案:

答案 0 :(得分:1)

必须将配置注入构造函数

@Inject
public OnStart(Config config) {