简体   繁体   中英

PlayFramework Scala dependency Injection Javax

I am new to Scala and the PlayFramework and am trying to figure out how I can do aa dependency Injection. I basically want a file that will be a trait and inject that into a controller. My problem is that my Controller class is not seeing my Trait this is my code

ProfileTrait

package traitss

import play.api.mvc._


trait ProfileTrait extends Controller {
    def Addone()
  }

Then I try to inject that into my controller

import java.nio.file.{Files, Paths}

import traitss.ProfileTrait_
import play.api.mvc.{Action, Controller}
import javax.inject._

class Profiles @Inject() (profileTrait: ProfileTrait)   extends Controller
{

}

However my controller is not seeing it, I am trying to follow the example here https://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection . I am using the play framework version 2.50

You cannot inject a trait directly. You need to specify the implementation of the trait that needs to be injected.

There are two ways to specify the implementation of a trait that is to be injected:

Using @ImplementedBy annotation. Here's a simple example:

package traitss

import play.api.mvc._
import com.google.inject.ImplementedBy

@ImplementedBy(classOf[ProfileTraitImpl])
trait ProfileTrait extends Controller {
    def Addone()
}

class ProfileTraitImpl extends ProfileTrait {
    // your implementation goes here
}

Using Programmatic Bindings

package traitss

import play.api.mvc._
import com.google.inject.ImplementedBy

@ImplementedBy(classOf[ProfileTraitImpl])
trait ProfileTrait extends Controller {
    def Addone()
}

ProfileTraitImpl:

package traitss.impl

class ProfileTraitImpl extends ProfileTrait {
    // your implementation goes here
}

Create a module where you can bind the implementation with trait

import com.google.inject.AbstractModule

class Module extends AbstractModule {
  def configure() = {

    bind(classOf[ProfileTrait])
      .to(classOf[ProfileTraitImpl])
  }
}

With using the modules approach you get an additional benefit of enabling or disabling the binding. For example, in your application.conf file you can enable/disable a module using play.modules.enabled += module OR play.modules.disabled += module

You can't inject a trait, you have to inject an object that implements that trait.

For dependency injection to work, you have to tell the framework (play uses Guice under the hood) how to resolve the dependency to be injected. There are many ways to do it and it depends on your case, for more detail you can look at Guice's documentation , the simplest way in play is to create Module.scala into your app directory , if it's not there already, and put something like this:

import com.google.inject.AbstractModule
class Module extends AbstractModule {
  override def configure() = {
    bind(classOf[ProfileTrait]).toInstance( ... )
  }
} 

where in ... you put the logic to create the object you want to inject.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM