plugins - 如何使用模块而不是插件插入播放生命周期?

标签 plugins module lifecycle playframework-2.4

我看到 Plugin 类现在已被弃用(从 2.4.x 版本开始)...在 api 文档中它说我应该使用模块...所以这就是我的问题。 如何编写模块,以及如何将该模块插入主应用程序的播放生命周期中?

最佳答案

您没有指定您使用的是哪种语言,因此我将快速介绍这两种语言。我的两个答案都基于以下存储库:

Java

  1. 按照您想要的方式编写您的功能 - 没有要扩展的特定类。如果您对 Play 组件有依赖关系,例如 Configuration ,你应该注入(inject)它们。

    @Singleton
    public class MyModuleCode {
    
        private final boolean enableWidgets;
    
        @javax.inject.Inject
        public MyModuleCode(final Configuration configuration) {
            this.enableWidgets = configuration.getBoolean("widgets.enabled", false);
        }
    }
    

请注意,依赖注入(inject)用于代替静态引用。另请注意,我已为本示例指定了 @Singleton注释,但也可以具有例如每个请求的范围。

参见the Play DI docs了解更多信息

  • 公开模块的组件。为此,请扩展 play.api.inject.Module类和实现 public Seq<Binding<?>> bindings(final Environment environment, final Configuration configuration) .

    package com.example.module;
    
    public class MyModule extends Module
    {
        @Override
        public Seq<Binding<?>> bindings(final Environment environment,
                                        final Configuration configuration)
        {
            return seq(bind(MyModuleCode.class).toSelf().in(Singleton.class));
        }
    }
    
  • 在这里,您还可以将实现绑定(bind)到接口(interface)、配置实例提供程序等。

  • 如果您要公开发布模块,我们假设您在此处执行此操作 - 这超出了问题的范围。我们还假设您已经在您正在处理的项目中添加了该模块的依赖项。

  • 启用 application.conf 中的模块.

    play {
        modules {
            enabled += com.example.module.MyModule
        }
    }
    
  • 通过模块公开的组件 - 只是 MyModuleCode在此示例中 - 现在可用于注入(inject)到您的 Controller 、操作等中。

  • 如果需要关闭钩子(Hook),只需注入(inject) ApplicationLifecycle进入组件并注册钩子(Hook);请参阅https://playframework.com/documentation/2.4.x/JavaDependencyInjection#Stopping/cleaning-up了解详情。

  • 斯卡拉

    1. 按照您想要的方式编写您的功能 - 没有要扩展的特定类。如果您对 Play 组件有依赖关系,例如 CacheApi ,你应该注入(inject)它们。

      @Singleton
      class DefaultPatternCache @Inject() (cache: CacheApi) extends PatternCache {
          override def apply(value: String): Option[Pattern] = cache.getOrElse[Option[Pattern]](key = s"Deadbolt.pattern.$value") { Some(Pattern.compile(value)) }
      }
      

    请注意,依赖注入(inject)用于代替静态引用。另请注意,我已为本示例指定了 @Singleton注释,但也可以具有例如每个请求的范围。

    参见the Play DI docs了解更多信息

  • 公开模块的组件。为此,请扩展 play.api.inject.Module类和实现 def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] .

    package com.example.module
    
    import com.example.module.cache.{DefaultPatternCache, PatternCache}
    import play.api.inject.{Binding, Module}
    import play.api.{Configuration, Environment}
    
    class MyModule extends Module {
        override def bindings(environment: Environment, configuration: Configuration): Seq[Binding[_]] = Seq(bind[PatternCache].to[DefaultPatternCache])
    }
    
  • 在这里,您还可以将实现绑定(bind)到特征、配置实例提供程序等。

  • 如果您要公开发布模块,我们假设您在此处执行此操作 - 这超出了问题的范围。我们还假设您已经在您正在处理的项目中添加了该模块的依赖项。

  • 启用 application.conf 中的模块.

    play {
        modules {
            enabled += com.example.module.MyModule
        }
    }
    
  • 通过模块公开的组件 - 只是 MyModuleCode在此示例中 - 现在可用于注入(inject)到您的 Controller 、操作等中。

  • 如果需要关闭钩子(Hook),只需注入(inject) ApplicationLifecycle进入组件并注册钩子(Hook);请参阅https://playframework.com/documentation/2.4.x/ScalaDependencyInjection#Stopping/cleaning-up了解详情。

  • 摘要

    模块不再有什么特别之处 - 它们只是对可注入(inject)组件进行分组的一种方式。

    关于plugins - 如何使用模块而不是插件插入播放生命周期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31623328/

    相关文章:

    android - 接收器生命周期 - 在 onResume() 中重新注册时崩溃

    Android - 为应用程序的所有 Activity 执行代码 onResume 和 onPause?

    android - 无法使用 Ionic 2 框架安装 cordova 相机插件

    javascript - 将选项传递给 ES6 模块导入

    c - 错误: dereferencing pointer to incomplete type -> passing structure to module

    javascript - 使用 webpack 加载揭示模块模式模块并将它们全局用作 es6 模块

    ios - 何时更新 CoreData 持久容器值

    java - 开发 eclipse 插件来解析 java 程序的程序

    c++ - QtPlugin 无法加载库

    asp.net-mvc - 如何在 ASP.net/ASP.net MVC 中开发基于可插拔/可安装模块的应用程序