【问题标题】:Mysterious Play 2.4 Injection exceptionMysterious Play 2.4 注入异常
【发布时间】:2015-09-23 11:55:48
【问题描述】:

最近升级到 Play 2.4,我还在学习所有的小怪癖等等。我正试图让我的索引页面正常工作,但我很难过,而且我知道这是我缺少的一些小东西。这是错误

CreationException: Unable to create injector, see the following errors:

1) Error in custom provider, Configuration error: Configuration error[Cannot connect to database [default]]
  while locating play.api.db.DBApiProvider
  while locating play.api.db.DBApi
    for parameter 0 at play.db.DefaultDBApi.<init>(DefaultDBApi.java:28)
  at play.db.DefaultDBApi.class(DefaultDBApi.java:28)
  while locating play.db.DefaultDBApi
  while locating play.db.DBApi
    for field at play.db.DBModule$NamedDatabaseProvider.dbApi(DBModule.java:61)
  while locating play.db.DBModule$NamedDatabaseProvider
  at com.google.inject.util.Providers$GuicifiedProviderWithDependencies.initialize(Providers.java:149)
  at play.db.DBModule.bindings(DBModule.java:40):

还有我的配置信息(是的,mysql的端口号是正确的)

应用程序.conf

db.default.driver=com.mysql.jdbc.Driver
db.default.url="jdbc:mysql:localhost:33060/coffee_bean"
db.default.username=
db.default.password=""
ebean.default = ["models.*"]

主控制器

public Result index() {
        ObjectNode response = Json.newObject();
        Configuration config = Play.application().configuration();
        response.put(config.getString("coffee.bean.message.key"),config.getString("coffee.bean.success.message"));
        response.put(config.getString("version"), config.getString("coffee.bean.version"));

        return ok(response);
    }

内置.sbt

name := """coffee-bean"""

version := "1.0-SNAPSHOT"

lazy val root = (project in file(".")).enablePlugins(PlayJava)

scalaVersion := "2.11.6"

libraryDependencies ++= Seq(
  javaJdbc,
  cache,
  javaWs,
  "mysql" % "mysql-connector-java" % "5.1.36",
  evolutions
)

// Play provides two styles of routers, one expects its actions to be injected, the
// other, legacy style, accesses its actions statically.
routesGenerator := InjectedRoutesGenerator
lazy val myProject = (project in file("."))
  .enablePlugins(PlayJava, PlayEbean)

plugins.sbt

// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.2")

// Web plugins
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.0.6")
addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.3")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.7")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0")

// Play enhancer - this automatically generates getters/setters for public fields
// and rewrites accessors of these fields to use the getters/setters. Remove this
// plugin if you prefer not to have this feature, or disable on a per project
// basis using disablePlugins(PlayEnhancer) in your build.sbt
addSbtPlugin("com.typesafe.sbt" % "sbt-play-enhancer" % "1.1.0")

// Play Ebean support, to enable, uncomment this line, and enable in your build.sbt using
// enablePlugins(SbtEbean). Note, uncommenting this line will automatically bring in
// Play enhancer, regardless of whether the line above is commented out or not.
addSbtPlugin("com.typesafe.sbt" % "sbt-play-ebean" % "1.0.0")
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.0")

还有什么需要看的吗?

到目前为止,我已经尝试重新导入依赖项,仔细检查以确保版本正确,再次查看文档,但到目前为止没有任何效果。我希望某个用户会得到所有的答案:)

【问题讨论】:

    标签: java guice ebean playframework-2.4


    【解决方案1】:

    Database connection configuration 不见了。

    定义在conf\application.conf:

    db.default.driver=com.mysql.jdbc.Driver
    db.default.url="jdbc:mysql://localhost/playdb"
    db.default.username=playdbuser
    db.default.password="a strong password"
    

    【讨论】:

    • 您确实为我指明了正确的方向。我的连接字符串写错了>.
    • 虽然将连接信息改成正确,但还是无法连接。不知道还有没有其他设置可以设置?我现在觉得自己像个白痴>.
    • 应该设置mysql数据库的用户名和密码。此链接将帮助您重置 mysl 的 root 密码:Mysql: How to Reset the Root Password
    【解决方案2】:

    我的配置:

    <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
                 version="2.1">
    
        <persistence-unit name="defaultPersistenceUnit" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <non-jta-data-source>caribbeanDS</non-jta-data-source>
            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
                <property name="hibernate.hbm2ddl.auto" value="update"/>
            </properties>
        </persistence-unit>
    
    </persistence>
    

    我的代码:

    version := "1.0-SNAPSHOT"
    
    lazy val root = (project in file(".")).enablePlugins(PlayJava)
    
    scalaVersion := "2.11.6"
    
    libraryDependencies ++= Seq(
      javaJdbc,
      cache,
      javaWs,
      javaJpa,
      "org.hibernate" % "hibernate-entitymanager" % "4.3.10.Final" // replace by your jpa implementation
    )
    
    
    // Play provides two styles of routers, one expects its actions to be injected, the
    // other, legacy style, accesses its actions statically.
    routesGenerator := InjectedRoutesGenerator
    
    
    fork in run := true
    
    
    # Default database configuration using MySQL database engine
    # Connect to playdb as playdbuser
    db.caribbean.driver=com.mysql.jdbc.Driver
    db.caribbean.url="jdbc:mysql://localhost:8889/bahamasnet"
    db.caribbean.username=root
    db.caribbean.password="root"
    
    db.caribbean.jndiName=caribbeanDS
    
    jpa.default=defaultPersistenceUnit
    

    【讨论】:

      猜你喜欢
      • 2015-10-08
      • 1970-01-01
      • 1970-01-01
      • 2016-02-26
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 2016-01-20
      • 1970-01-01
      相关资源
      最近更新 更多