gpt4 book ai didi

dependency-injection - Google Guice 的隐藏功能

转载 作者:行者123 更新时间:2023-12-03 10:08:53 24 4
gpt4 key购买 nike

就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the help center寻求指导。




10年前关闭。




Google Guice 提供了一些很棒的依赖注入(inject)功能。

我遇到了 @Nullable 最近的功能允许您将构造函数参数标记为可选(允许为空),因为 Guice 默认情况下不允许这些:

例如

public Person(String firstName, String lastName, @Nullable Phone phone) {
this.firstName = checkNotNull(firstName, "firstName");
this.lastName = checkNotNull(lastName, "lastName");
this.phone = phone;
}

https://github.com/google/guice/wiki/UseNullable

人们使用的 Guice 的其他有用特性是什么(尤其是不太明显的特性)?

最佳答案

它们都不是要隐藏的,但这些是我在 Guice 中最喜欢的“奖励功能”:

  • 桂策可以inject a TypeLiteral<T> ,有效地击败删除。
  • TypeLiteral可以做generic type resolution : 这告诉你 get()List<String>返回 Iterator<String> .
  • Types是实现 Java 的泛型类型接口(interface)的工厂。
  • Grapher可视化喷油器。如果您的自定义提供程序实现 HasDependencies ,它可以扩充这个图。
  • Modules.override()在紧要关头非常方便。
  • 定义参数化键的简短语法:new Key<List<String>>() {} .
  • Binder.skipSources()允许您编写其错误消息正确跟踪行号的扩展。
  • The SPI . Elements.getElements()将模块分解为原子和 Elements.getModule()将它们重新组合在一起。
  • 如果您实现 equals()hashCode()Module ,您可以多次安装该模块而不会出现问题。
  • 关于dependency-injection - Google Guice 的隐藏功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2716993/

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