gpt4 book ai didi

scala - 玩 2.5 剪影 4 - 带有 guice 的 DI

转载 作者:行者123 更新时间:2023-12-05 00:53:55 25 4
gpt4 key购买 nike

语言:斯卡拉;
框架:玩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 重写这些绑定(bind)吗?

另外我也有这个代码:
@Provides
def provideEnvironment(
userService: UserService,
authenticatorService: AuthenticatorService[CookieAuthenticator],
eventBus: EventBus
): Environment[MyEnv] = {
Environment[MyEnv](
userService,
authenticatorService,
Seq(),
eventBus
)
}

提前致谢。

最佳答案

感谢 insan-e指向正确的方向。这是显示如何使用 guice 注入(inject)通用实现的答案:

Inject Generic Implementation using Guice

因此,如果从方程中删除 scala-guice 库,绑定(bind)可以这样写:

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]]{})

关于scala - 玩 2.5 剪影 4 - 带有 guice 的 DI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40472317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com