简体   繁体   English

PlayFramework Scala依赖注入Javax

[英]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. 我是Scala和PlayFramework的新手,正在尝试弄清如何进行依赖注入。 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 我的问题是我的Controller类没有看到我的Trait,这是我的代码

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 . 但是我的控制器没有看到它,我尝试按照此处的示例https://www.playframework.com/documentation/2.5.x/ScalaDependencyInjection进行操作 I am using the play framework version 2.50 我正在使用播放框架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. 使用@ImplementedBy批注。 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: 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 例如,在application.conf文件中,可以使用play.modules.enabled += module play.modules.disabled += module启用/禁用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. 为了使依赖项注入起作用,您必须告诉框架(后台使用Guice玩游戏)如何解决要注入的依赖项。 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: 有很多方法可以执行此操作,具体取决于您的情况,有关更多详细信息 ,请参阅Guice的文档 ,最简单的方法是在app目录中创建Module.scala (如果尚不存在),然后放入类似这个:

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. ...何处放置逻辑以创建要注入的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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