Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guice and Play2 Singleton from trait

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.

like image 473
Gleeb Avatar asked Feb 17 '16 12:02

Gleeb


3 Answers

Use below import, instead of import javax.inject.Singleton

import com.google.inject.{Inject, Singleton}
like image 153
Ashish Pushp Avatar answered Oct 04 '22 22:10

Ashish Pushp


As pointed out by @Tavian-Barnes, the solution is to ensure you have the following import:

import javax.inject.Singleton
like image 25
Chris Beach Avatar answered Oct 04 '22 20:10

Chris Beach


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

like image 39
swapnil shashank Avatar answered Oct 04 '22 21:10

swapnil shashank