代码之家  ›  专栏  ›  技术社区  ›  Dan

CreationException:无法在Play 2.5.18中创建喷油器错误,以替换使用com的GlobalSettings Java代码。谷歌。注射抽象模块

  •  6
  • Dan  · 技术社区  · 6 年前

    我正在将我的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

    以下是错误中提到的类:

    全球的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();
        }
    
    }
    

    启动。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 回复  |  直到 6 年前
        1
  •  2
  •   Andriy Kuba    6 年前

    必须将配置注入构造函数

    @Inject
    public OnStart(Config config) {