播放2.5.12获取错误ProvisionException:无法提供com.typesafe.config.Config的绑定没有实现

时间:2018-04-04 16:42:33

标签: java playframework typesafe-config

我已将所有play.Configuration代码升级为com.typesafe.config.Config个类。我正在使用Play Framework 2.5.12运行应用程序。

我现在正在使用:

import com.typesafe.config.Config;
...
    @Inject
    static Config config;

我可以cleancompile代码,但当我run应用程序时,我收到此错误:

ProvisionException: Unable to provision, see the following errors:

1) No implementation for com.typesafe.config.Config was bound.
  while locating com.typesafe.config.Config
    for field at controllers.IntakeController.config(IntakeController.java:66)
  while locating controllers.IntakeController
    for parameter 3 at router.Routes.<init>(Routes.scala:80)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router
    for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:200)
  while locating play.api.http.JavaCompatibleHttpRequestHandler
  while locating play.api.http.HttpRequestHandler
    for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
  at play.api.DefaultApplication.class(Application.scala:221)
  while locating play.api.DefaultApplication
  while locating play.api.Application

2) No implementation for com.typesafe.config.Config was bound.
  while locating com.typesafe.config.Config
    for field at controllers.account.Reset.config(Reset.java:43)
  while locating controllers.account.Reset
    for parameter 7 at router.Routes.<init>(Routes.scala:80)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router
    for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:200)
  while locating play.api.http.JavaCompatibleHttpRequestHandler
  while locating play.api.http.HttpRequestHandler
    for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
  at play.api.DefaultApplication.class(Application.scala:221)
  while locating play.api.DefaultApplication
  while locating play.api.Application

3) No implementation for com.typesafe.config.Config was bound.
  while locating com.typesafe.config.Config
    for field at controllers.account.Signup.config(Signup.java:50)
  while locating controllers.account.Signup
    for parameter 6 at router.Routes.<init>(Routes.scala:80)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router
    for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:200)
  while locating play.api.http.JavaCompatibleHttpRequestHandler
  while locating play.api.http.HttpRequestHandler
    for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
  at play.api.DefaultApplication.class(Application.scala:221)
  while locating play.api.DefaultApplication
  while locating play.api.Application

3 errors

其余的错误说明:

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

1) No implementation for com.typesafe.config.Config was bound.
  while locating com.typesafe.config.Config
    for field at controllers.IntakeController.config(IntakeController.java:66)
  while locating controllers.IntakeController
    for parameter 3 at router.Routes.<init>(Routes.scala:80)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router
    for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:200)
  while locating play.api.http.JavaCompatibleHttpRequestHandler
  while locating play.api.http.HttpRequestHandler
    for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
  at play.api.DefaultApplication.class(Application.scala:221)
  while locating play.api.DefaultApplication
  while locating play.api.Application

2) No implementation for com.typesafe.config.Config was bound.
  while locating com.typesafe.config.Config
    for field at controllers.account.Reset.config(Reset.java:43)
  while locating controllers.account.Reset
    for parameter 7 at router.Routes.<init>(Routes.scala:80)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router
    for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:200)
  while locating play.api.http.JavaCompatibleHttpRequestHandler
  while locating play.api.http.HttpRequestHandler
    for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
  at play.api.DefaultApplication.class(Application.scala:221)
  while locating play.api.DefaultApplication
  while locating play.api.Application

3) No implementation for com.typesafe.config.Config was bound.
  while locating com.typesafe.config.Config
    for field at controllers.account.Signup.config(Signup.java:50)
  while locating controllers.account.Signup
    for parameter 6 at router.Routes.<init>(Routes.scala:80)
  while locating router.Routes
  while locating play.api.inject.RoutesProvider
  while locating play.api.routing.Router
    for parameter 0 at play.api.http.JavaCompatibleHttpRequestHandler.<init>(HttpRequestHandler.scala:200)
  while locating play.api.http.JavaCompatibleHttpRequestHandler
  while locating play.api.http.HttpRequestHandler
    for parameter 4 at play.api.DefaultApplication.<init>(Application.scala:221)
  at play.api.DefaultApplication.class(Application.scala:221)
  while locating play.api.DefaultApplication
  while locating play.api.Application

3 errors
     com.google.inject.internal.InjectorImpl$2.get(InjectorImpl.java:1025)
     com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051)
     play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:405)
     play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:400)
     play.api.inject.guice.GuiceApplicationBuilder.build(GuiceApplicationBuilder.scala:123)
     play.api.inject.guice.GuiceApplicationLoader.load(GuiceApplicationLoader.scala:21)
     play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1$$anonfun$2.apply(DevServerStart.scala:168)
     play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1$$anonfun$2.apply(DevServerStart.scala:164)
     play.utils.Threads$.withContextClassLoader(Threads.scala:21)
     play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:164)
     play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1$$anonfun$1.apply(DevServerStart.scala:131)
     scala.Option.map(Option.scala:146)
     play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:131)
     play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1$$anonfun$apply$1.apply(DevServerStart.scala:129)
     scala.util.Success.flatMap(Try.scala:231)
     play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:129)
     play.core.server.DevServerStart$$anonfun$mainDev$1$$anon$1$$anonfun$get$1.apply(DevServerStart.scala:121)
     scala.concurrent.impl.Future$PromiseCompletingRunnable.liftedTree1$1(Future.scala:24)
     scala.concurrent.impl.Future$PromiseCompletingRunnable.run(Future.scala:24)
     java.util.concurrent.ForkJoinTask$RunnableExecuteAction.exec(ForkJoinTask.java:1402)
     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)

我发现了几个帖子:

No implementation for play.api.db.slick.DatabaseConfigProvider was bound

Play error on startup: No implementation for play.api.db.Database was bound

但是他们没有帮助我。

我确实找到了一个解决方案是添加此帖子的帖子:

play.modules.enabled += com.typesafe.config.Config

application.conf文件。我有一种感觉它不会起作用,但事实并非如此。

任何帮助都会很棒。

---------------------------编辑4/5/18 -------------- --------------

我在代码中添加了一个模块和提供程序: 模块:

package module;

import com.google.inject.AbstractModule;
import com.typesafe.config.Config;

public class ConfigModule extends AbstractModule {
  @Override
  protected void configure() {
    bind(Config.class).toProvider(providers.ConfigProvider.class);
  }
}

提供者:

package providers;

import com.typesafe.config.Config;

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

@Singleton
public class ConfigProvider implements Provider<Config> {

  private final Config configuration;

  @Inject
  public ConfigProvider(Config configuration) {
    this.configuration = configuration;
  }

  @Override
  public Config get() {
    return this.configuration.getConfig("application.conf");
  }

}

我还补充说:

playmodules.enabled += "modules.ConfigModule"

application.conf文件。

在此之后,我仍然得到同样的错误。

2 个答案:

答案 0 :(得分:0)

您必须为com.typesafe.config.Config提供一个实现,因为这只是一个接口。

ConfigFactory执行此操作,您必须使用Factory Provider(Guice)

就个人而言,我只是使用Factory静态创建Config:

 def config(): Config = {
    ConfigFactory.invalidateCaches()
    ConfigFactory.load()
  }

在这里看全班:AdaptersContext.scala

答案 1 :(得分:0)

我在从 Play 2.3 迁移到 Play 2.4 时遇到了类似的问题(我的目标是逐步将其迁移到 2.7),而新的 play-mailer 开始抱怨同样的问题。

修复

  1. 创建一个像下面这样的模块,你可能已经有了这样的

  2. 添加将提供 Conf 对象的@provides。

    package modules;
    
    import com.google.inject.AbstractModule;
    import com.google.inject.Provides;
    import com.typesafe.config.Config;
    import com.typesafe.config.ConfigFactory;
    
    public class JavaModules extends AbstractModule {
    
        @Override
        protected void configure() {
    
        }
    
        @Provides
        public Config ProvideConfigFactory() {
            return ConfigFactory.load("application.conf");
        }
    }
    

**在application.conf中连接模块**

(如果您的问题不是由邮件程序模块引起的,请省略该行)。

play.modules {
        enabled += "modules.JavaModules"
        enabled += "play.api.libs.mailer.MailerModule"
    }