I am using Play with Scala and I am trying to create a singleton, and i want to inject it from its trait and not directly.
for example:
@ImplementedBy(classOf[S3RepositoryImpl])
trait S3Repository {
}
@Singleton
class S3RepositoryImpl extends S3Repository {
}
But this fails with error:
trait Singleton is abstract; cannot be instantiated
I have tried several combinations and they all produce the same.
I come from Spring background and its very natural there? am i missing something about how Guice handles this type of Injection?
Thanks.
正如@Tavian-Barnes 所指出的,解决方案是确保您具有以下导入:
import javax.inject.Singleton
I have here a "complete" working example, just hope that I'm not stating the obvious...
package controllers
import play.api._
import play.api.mvc._
import com.google.inject._
class Application @Inject() (s3: S3Repository) extends Controller {
def index = Action {
println(s3.get)
Ok
}
}
@ImplementedBy(classOf[S3RepositoryImpl])
trait S3Repository {
def get: String
}
@Singleton
class S3RepositoryImpl extends S3Repository {
def get: String = "bla"
}
Whenever you mark a class' constructor with @Inject
the Guice will manage the injection of the instance itself. So, if you marked your class as @Singleton
, Guice will create and will always give you just that one instance. Nobody can stop you from manually instantiating a class in your code... You can explore it in detail at Play .
使用下面的导入,而不是import javax.inject.Singleton
import com.google.inject.{Inject, Singleton}
Import both Inject and Singleton.
import javax.inject.{Inject, Singleton}
Refrain from using:
import com.google.inject.{Inject, Singleton}
as play framework requires the javax import
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.