playframework 2.4 基本ebean配置

Posted

技术标签:

【中文标题】playframework 2.4 基本ebean配置【英文标题】:playframework 2.4 basic ebean configuration 【发布时间】:2015-11-12 17:46:57 【问题描述】:

我正在尝试将 play 项目从 2.3 转换为 2.4,并设法按照迁移指南对其进行编译。

但是我不确定如何获得基本的工作 mysql 数据库连接。 在运行时,它会为我提供堆栈跟踪页面,如下面粘贴的输出。

build.sbt

lazy val root = (project in file(".")).enablePlugins(PlayJava, PlayEbean)
...
"org.the" % "model-dal" % "1.2.3-SNAPSHOT",

application.conf

play.evolutions.enabled=false
ebean.thedb=["models.theapp.*"]

创建了一个 ebean.properties 文件:

datasource.thedb.username=user
datasource.thedb.password=password
datasource.thedb.databaseUrl=jdbc:mysql://127.0.0.1:3306/thedb
datasource.thedb.databaseDriver=com.mysql.jdbc.Driver

plugins.sbt 条目

addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "2.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0")

堆栈跟踪

    ! @6o56g5bnj - Internal server error, for (GET) [/] ->

    play.api.UnexpectedException: Unexpected exception[CreationException: Unable to create injector, see the following errors:

    1) Error in custom provider, Configuration error: Configuration error[null]
      while locating play.db.ebean.DefaultEbeanConfig$EbeanConfigParser
      at play.db.ebean.EbeanModule.bindings(EbeanModule.java:24):
    Binding(interface play.db.ebean.EbeanConfig to ProviderConstructionTarget(class play.db.ebean.DefaultEbeanConfig$EbeanConfigParser) eagerly) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
      while locating play.db.ebean.EbeanConfig
        for parameter 0 at play.db.ebean.EbeanDynamicEvolutions.<init>(EbeanDynamicEvolutions.java:36)
      at play.db.ebean.EbeanDynamicEvolutions.class(EbeanDynamicEvolutions.java:33)
      while locating play.db.ebean.EbeanDynamicEvolutions
      at play.db.ebean.EbeanModule.bindings(EbeanModule.java:23):
    Binding(class play.api.db.evolutions.DynamicEvolutions to ConstructionTarget(class play.db.ebean.EbeanDynamicEvolutions) eagerly) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
      while locating play.api.db.evolutions.DynamicEvolutions
      while locating play.api.db.evolutions.ApplicationEvolutionsProvider
      at play.api.db.evolutions.EvolutionsModule.bindings(EvolutionsModule.scala:22):
    Binding(class play.api.db.evolutions.ApplicationEvolutions to ProviderConstructionTarget(class play.api.db.evolutions.ApplicationEvolutionsProvider) eagerly) (via modules: com.google.inject.util.Modules$OverrideModule -> play.api.inject.guice.GuiceableModuleConversions$$anon$1)
      while locating play.api.db.evolutions.ApplicationEvolutions
    Caused by: Configuration error: Configuration error[null]
            at play.api.Configuration$.configError(Configuration.scala:178)
            at play.api.Configuration.reportError(Configuration.scala:829)
            at play.Configuration.reportError(Configuration.java:351)
            at play.db.ebean.DefaultEbeanConfig$EbeanConfigParser.parse(DefaultEbeanConfig.java:81)
            at play.db.ebean.DefaultEbeanConfig$EbeanConfigParser.get(DefaultEbeanConfig.java:60)
            at play.db.ebean.DefaultEbeanConfig$EbeanConfigParser.get(DefaultEbeanConfig.java:44)
            at com.google.inject.internal.ProviderInternalFactory.provision(ProviderInternalFactory.java:81)
            at com.google.inject.internal.BoundProviderFactory.provision(BoundProviderFactory.java:72)
            at com.google.inject.internal.ProviderInternalFactory.circularGet(ProviderInternalFactory.java:61)
            at com.google.inject.internal.BoundProviderFactory.get(BoundProviderFactory.java:62)
            at com.google.inject.internal.ProviderToInternalFactoryAdapter$1.call(ProviderToInternalFactoryAdapter.java:46)
            at com.google.inject.internal.InjectorImpl.callInContext(InjectorImpl.java:1103)
            at com.google.inject.internal.ProviderToInternalFactoryAdapter.get(ProviderToInternalFactoryAdapter.java:40)
            at com.google.inject.internal.SingletonScope$1.get(SingletonScope.java:145)
            at com.google.inject.internal.InternalFactoryToProviderAdapter.get(InternalFactoryToProviderAdapter.java:41)
            at com.google.inject.internal.SingleParameterInjector.inject(SingleParameterInjector.java:38)

【问题讨论】:

【参考方案1】:

(1) 在 application.conf 中需要禁用进化,否则会出现上述异常。 play.evolutions.enabled=false

(2) play db中需要数据库配置。 application.conf 文件中的属性,eb​​ean.properties 文件不是必需的。

应用程序.conf

db.thedb.username=user
db.thedb.password=password
db.thedb.databaseUrl=jdbc:mysql://127.0.0.1/thedb
db.thedb.databaseDriver=com.mysql.jdbc.Driver

【讨论】:

以上是关于playframework 2.4 基本ebean配置的主要内容,如果未能解决你的问题,请参考以下文章

Play 2.4 - 在 Eager 单例模块中使用 Ebean

如何在 playframework 中使用依赖注入创建 Ebeanserver?

Play Framework 2 Ebean 为字段指定默认值

Play Framework - Ebean - 没有为类“类名”注册 ScalarType

包 io.ebean 不存在 Play Framework 2.7

Play Framework 2.3.8 中与 Ebeans 的 OneToOne 关系