java 用 javax.inject 替换 com.google.inject

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7837010/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:37:58  来源:igfitidea点击:

Replacing com.google.inject with javax.inject

javagwtdependency-injectionguicegin

提问by Blessed Geek

Is it true that javax.inject annotations can function as direct replacements for com.google.inject?

javax.inject 注释真的可以直接替代 com.google.inject 吗?

So that, if I replaced all my current guice/gin annotations with those from javax.inject, my app would compile and run just fine?

所以,如果我用 javax.inject 中的那些替换了我当前所有的 guice/gin 注释,我的应用程序会编译并运行得很好吗?

First, does javax.inject cover all the bases that google.inject cover?

首先,javax.inject 是否涵盖了 google.inject 涵盖的所有基础?

采纳答案by Bozho

Yes, it will work fine. In fact the author of guice (Bob Lee) is a spec-lead for the javax.injectspecification.

是的,它会正常工作。事实上,guice 的作者(Bob Lee)是该规范的javax.inject规范负责人。

javax.inject.Injectdoes not have the optionalattribute, so if you want an optional dependency, you'd have to use the guice annotation.

javax.inject.Inject没有optional属性,所以如果你想要一个可选的依赖,你必须使用 guice 注释。

回答by jfpoilpret

Actually, I have chosen to refrain from switching to javax.inject, because I find the spec much minimalistic in comparison to what Guice provides (which I use):

实际上,我选择避免切换到 javax.inject,因为与 Guice 提供的(我使用的)相比,我发现该规范非常简约:

  • @Optionalas mentioned by @Bozho
  • @ImplementedBywhich is very useful when you want to reduce the number of explicit bindings (for code clarity) and when you want to be able to easily override the default @ImplementedBybinding if you need (e.g. for integration tests).
  • @Optional正如@Bozho 所提到的
  • @ImplementedBy当您想要减少显式绑定的数量(为了代码清晰)以及当您希望能够根据需要轻松覆盖默认@ImplementedBy绑定(例如,用于集成测试)时,这非常有用。

There are probably others but for me these 2 are showstoppers already.

可能还有其他人,但对我来说,这两个已经是引人注目的了。

回答by APISonar

The question is quite old but... FYI: you can automatically check that on apiwave.

这个问题很老,但是......仅供参考:您可以在 apiwave 上自动检查

By learning with past changes we see that com.google.inject.Injectcan be replaced by javax.inject.Inject.

通过学习过去的变化,我们看到com.google.inject.Inject可以用 代替javax.inject.Inject