简体   繁体   English

在示例播放剪影模板中定义隐式环境值(使用Guice)

[英]Definition of implicit environment value in example play-silhouette template (using Guice)

In the silhouette implementation example found here , how in the template is the implicit 'env' value (of type Environment[User, CachedCookieAuthenticator]) used in line 28 of /app/controllers/SignUpController.scala, for example, defined using Guice? 此处找到的剪影实现示例中,例如,如何在模板中的/app/controllers/SignUpController.scala的第28行中使用隐式“ env”值(Environment [User,CachedCookieAuthenticator]类型)(例如,使用Guice定义)?

I guess I do not understand how provideEnvironment in app/utils/di/SilhouetteModule.scala is used to "inject" the Silhouette Enviroment into SignUpController (for example) via the "injector" created in line 24 of app/Global.scala. 我想我不明白如何使用app / utils / di / SilhouetteModule.scala中的ProvideEnvironment通过在app / Global.scala第24行中创建的“注入器”将“剪影”环境“注入” SignUpController(例如)。 I don't see provideEnvironment being used anywhere in the play-silhouette-slick-seed example, so I can't seem to figure out what values are given to its arguments (such as userService, authenticatorService etc). 我看不到在play-silhouette-slick-seed示例中的任何地方都使用了provideEnvironment,因此我似乎无法弄清楚为其参数提供了哪些值(例如userService,authenticatorService等)。

This example of silhouette module use Guice for scala Dependency Injection framework. 这个剪影模块的示例使用Guice进行scala依赖注入框架。 All bindings are configured in util.di.SilhouetteModule.scala file. 所有绑定均在util.di.SilhouetteModule.scala文件中配置。 There is another example where Dependency Injection is replaced by Cake pattern. 还有另一个示例,其中依赖注入被Cake模式代替。 look at this 看这个

[edited]In short: 简而言之:

If you look at the Global.scala file, you'll find the guice configuration. 如果查看Global.scala文件,则会找到guice配置。 Guice is forced to create every controller. Guice被迫创建每个控制器。 Every view is dependend on controllers and will be managed by guice too. 每个视图都取决于控制器,也将由guice管理。

The SilhouetteModule.scala file, as was mentioned above, is for configuration Silhouette module. 如上所述, SilhouetteModule.scala文件用于配置Silhouette模块。 There are few methods annotated with @Provides . @Provides注释的方法@Provides If you look at the Guice documentation . 如果您查看Guice文档 Such method is called by Guice every time it needs class type the method returns, for instance: each time guice need to inject Environment[User, CachedCookieAuthenticator] it call def provideEnvironment method because this method return such type. Guice每次需要该方法返回的类类型时,便会调用该方法,例如:每次guice需要注入Environment[User, CachedCookieAuthenticator]它都会调用def provideEnvironment方法,因为该方法返回了此类。

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

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