gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:52:12 24 4
gpt4 key购买 nike

我看到 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/

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