I'm trying to store some data in a PostgreSQL database. This could be done by a tiny little Form rendered by PlayFramework. I want to test the Slick plugin to have some experience with it, but I'm getting stuck in this exception:
ProvisionException: Unable to provision, see the following errors:
1) Could not find a suitable constructor in services.Consumptions.
Classes must have either one (and only one) constructor annotated with
@Inject or a zero-argument constructor that is not private.
at services.Consumptions.class(Tables.scala:17)
while locating services.Consumptions
for parameter 0 at controllers.HouseSummary.<init>
(HouseSummary.scala:18)
while locating controllers.HouseSummary
for parameter 3 at router.Routes.<init>(Routes.scala:39)
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
what im doing wrong? My project/plugins.sbt
is:
// The Play plugin
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.5.10")
// web plugins
addSbtPlugin("com.typesafe.sbt" % "sbt-coffeescript" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-less" % "1.1.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-jshint" % "1.0.4")
addSbtPlugin("com.typesafe.sbt" % "sbt-rjs" % "1.0.8")
addSbtPlugin("com.typesafe.sbt" % "sbt-digest" % "1.1.1")
addSbtPlugin("com.typesafe.sbt" % "sbt-mocha" % "1.1.0")
addSbtPlugin("org.irundaia.sbt" % "sbt-sassify" % "1.4.6")
The main problem is that this works fine and save all in the PostgreSQL database:
object MyApp extends App {
val query = TableQuery[Consumptions]
val db = Database.forConfig("pg-postgres")
try {
Await.result(
db.run(DBIO.seq(query += consumption(0, DateTime.now(), 50.5,50.5,50.5,50.5))),
Duration.Inf
)
} finally {
db.close
}
}
This is the controller:
@Singleton
class HouseSummary @Inject()(consumptions: Consumptions) extends Controller {
val query = TableQuery[Consumptions]
val db = Database.forConfig("pg-postgres")
def add = Action { implicit request =>
summaryForm.bindFromRequest.fold(
formWithErrors => {
// binding failure, you retrieve the form containing errors:
BadRequest(formWithErrors.toString)
},
summaryData => {
/* binding success, you get the actual value. */
val newEntry = AddSummaryRequest(
summaryData.date,
summaryData.waterconsumption,
summaryData.electricityheater_1,
summaryData.electricityheater_2,
summaryData.electricitymain
)
Consumptions.add(newEntry)
try {
Await.result(db.run(DBIO.seq(
query += consumption(
0,
new DateTime(newEntry.date),
summaryData.waterconsumption,
summaryData.electricityheater_1, summaryData.electricitymain,
summaryData.electricityheater_2
)
)), Duration.Inf)
} finally db.close
Ok(s"Erfolgreich gespeichert für das Datum: ${summaryData.date}")
}
)
}
def summaryForm = Form(
mapping(
"date" -> date,
"waterconsumption" -> of[Double],
"electricityheater_1" -> of[Double],
"electricityheater_2" -> of[Double],
"electricitymain" -> of[Double]
)(AddSummaryRequest.apply)(AddSummaryRequest.unapply))
}
I found the error. I have tried to Inject the Consumption class and that dont work if the class you want to inject haven't a Constructor or something like that because guice don't know how to inject this class.
Change this:
class HouseSummary @Inject()(consumptions: Consumptions) extends Controller
To that:
class HouseSummary @Inject()() extends Controller
fixed the Problem
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.