yukuan
yukuan

Reputation: 541

Play framework 2.1.3 with guice

I know play provide the Global#getControllerInstance to support guice DI to Controller class.

But this way seems only work for Controller class, now I want to inject some of my service class into other class(not the class extend Controller). how can I do it?

I google a lot and cannot found the solution yet...

Upvotes: 1

Views: 1116

Answers (1)

Andrew Conner
Andrew Conner

Reputation: 1436

Once you have your modules set up (the blog post linked above is helpful), you can just use direct injection or constructor injection as you normally would, unrelated to Play!. Constructor injection example:

class SomeClass @Inject() (dependencyA: DependencyA, userRepo: UserRepo) {
  // then, use `dependencyA` and `userRepo`
} 

Then, from inside a controller, just inject an instance of SomeClass.

See scala-guice for a better API in Scala for guice.

Upvotes: 2

Related Questions