将 com.google.inject 替换为 javax.inject
javax.inject 注释是否可以直接替代 com.google.inject?
那么,如果我用 javax.inject 中的注释替换当前的所有 guice/gin 注释,我的应用程序将编译并运行得很好?
首先,javax.inject 是否涵盖了 google.inject 涵盖的所有基础?
Is it true that javax.inject annotations can function as direct replacements for 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?
First, does javax.inject cover all the bases that google.inject cover?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
是的,它会工作得很好。事实上,guice 的作者 (Bob Lee) 是 javax.inject 规范的规范领导者。
javax.inject.Inject
没有optional
属性,因此如果您想要可选依赖项,则必须使用 guice 注释。Yes, it will work fine. In fact the author of guice (Bob Lee) is a spec-lead for the
javax.inject
specification.javax.inject.Inject
does not have theoptional
attribute, so if you want an optional dependency, you'd have to use the guice annotation.实际上,我选择不切换到 javax.inject,因为我发现与 Guice 提供的(我使用的)相比,该规范非常简约:
@Optional
@ImplementedBy
当您想要减少显式绑定的数量(为了代码清晰)并且当您希望能够轻松覆盖默认的@ImplementedBy
绑定(如果需要)时,这非常有用(例如用于集成测试)。可能还有其他的,但对我来说,这两个已经是最精彩的了。
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):
@Optional
as mentioned by @Bozho@ImplementedBy
which 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@ImplementedBy
binding if you need (e.g. for integration tests).There are probably others but for me these 2 are showstoppers already.
请参阅Guice 网站上的 JSR-330 集成。
See JSR-330 Integration on Guice's site.