语言:
Scala;
框架:播放2.5;
图书馆:剪影4.0,Guice,scala-guice.
框架:播放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]]{})