加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

斯卡拉 – 玩2.5剪影4 – DI与guice

发布时间:2020-12-16 18:09:02 所属栏目:安全 来源:网络整理
导读:语言: Scala; 框架:播放2.5; 图书馆:剪影4.0,Guice,scala-guice. 其中一个官方的Silhouette种子项目使用guice和scala-guice(net.codingwell.scalaguice.ScalaModule)来编写DI配置.代码如下所示: import net.codingwell.scalaguice.ScalaModuleclass Modu
语言: Scala;
框架:播放2.5;
图书馆:剪影4.0,Guice,scala-guice.

其中一个官方的Silhouette种子项目使用guice和scala-guice(net.codingwell.scalaguice.ScalaModule)来编写DI配置.代码如下所示:

import net.codingwell.scalaguice.ScalaModule

class Module extends AbstractModule with ScalaModule{

  /**
    * Configures the module.
    */
  def configure() {

    bind[Silhouette[MyEnv]].to[SilhouetteProvider[MyEnv]]
    bind[SecuredErrorHandler].to[ErrorHandler]
    bind[UnsecuredErrorHandler].to[ErrorHandler]
    bind[IdentityService[User]].to[UserService]

我想知道,如果没有来自net.codingwell.scalaguice库的魔法,这段代码怎么样?有人可以仅使用原始guice重写这些绑定吗?

另外我还有这段代码:

@Provides
  def provideEnvironment(
      userService: UserService,authenticatorService: AuthenticatorService[CookieAuthenticator],eventBus: EventBus
  ): Environment[MyEnv] = {
    Environment[MyEnv](
      userService,authenticatorService,Seq(),eventBus
    )
  }

提前致谢.

解决方法

感谢 insan-e指向正确的方向.以下是显示如何使用guice注入通用实现的答案:

Inject Generic Implementation using Guice

因此,如果从等式中删除scala-guice库,绑定可以这样写:

import com.google.inject.{AbstractModule,Provides,TypeLiteral}
class Module extends AbstractModule {

  /**
    * Configures the module.
    */
  def configure() {
    bind(new TypeLiteral[Silhouette[MyEnv]]{}).to(new TypeLiteral[SilhouetteProvider[MyEnv]]{})

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读