Guice:我必须用@Inject注释对象图的每个类吗?
我想引入 Guice 以用于现有的中型项目。 对于我的需求,我需要一个自定义范围(会话太大,而对于我的项目来说请求很小)。
想象一下,我请求为我提供A类的实例,它对许多其他类(组合)具有直接和间接依赖关系。
我的自定义提供程序能够提供用作所有相关类的构造函数参数的类实例。
问题:
- 我真的必须在所有涉及的类的构造函数上添加
@Inject
(和我的自定义范围)注释吗?或者有办法 > 该指南只需要我请求的顶级类上的这些注释,并且所有进一步的依赖项都通过“询问”我的依赖提供者的自定义范围来解决类型?
如果这是真的,这将增加引入Guice是因为我要调整1000多个类。在介绍 guice 过程中提供的任何帮助和经验都值得赞赏。
I'd like to introduce Guice for the use of an existing mid-sized project.
For my demands I need a custom scope (session is too big, while request to small for my project).
Imagine that I request guice to provide me an instance of Class A which has direct and indirect dependencies to many other classes (composition).
My custom provider is able to provide the instance of classes which are used as constructor arguments of all involved classes.
Question:
- Do I really have to put an
@Inject
(and my custom scope) annotation on the constructors of all involved classes or is there a way that guice only requires these annotations on the top-level class which I request and that all further dependencies are resolved by "asking" my custom scope for a provider of the dependent types?
If this is true this would increase the effort of introducing Guice because I have to adjust more than 1000 classes. Any help and experiences during the introduction of guice is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,可以使用 Guice,而无需在任何地方放置
@Inject
注释。 Guice 支持提供商绑定、@Provides 方法 和 构造函数绑定,所有这些都允许您根据自己的选择绑定类型。然而,对于其正常操作,它需要 @Inject 注释作为元数据,告诉它类需要哪些依赖项以及可以在哪里注入它们。这样做的原因是,否则,它无法确定地告诉应该注入什么以及在哪里注入。例如,类可能有多个构造函数,而 Guice 需要某种不依赖任何猜测的方式来选择一个构造函数来注入。你可能会说“好吧,我的类只有一个构造函数,所以它不需要
@Inject
”,但是当有人向类添加新的构造函数时会发生什么?然后 Guice 就不再有决定的依据,应用程序就会崩溃。此外,这一切都假设您只进行构造函数注入。虽然构造函数注入通常是最好的选择,但 Guice 也允许注入方法(和字段),并且需要显式指定类的注入点的问题更加严重,因为大多数类都会有许多不存在的方法。用于注射,最多有几种。除了 @Inject 在告诉 Guice 方面的重要性之外,它还可以作为如何使用类的文档——该类是应用程序依赖注入有线基础设施的一部分。它还有助于在整个类中应用
@Inject
注释时保持一致,即使目前对于某些仅使用单个构造函数的类来说并不是绝对必要的。我还想指出,您可以使用 JSR-330@javax。如果标准 Java 注释比 Guice 特定的注释更适合您,请在 Guice 3.0 中注入.Inject 注释。
我不太清楚您询问提供商的范围是什么意思。作用域本身通常不创建对象;它们控制何时向无作用域的依赖项提供者询问新实例以及如何控制该实例的范围。当然,提供商是他们运作方式的一部分,但我不确定这是否是您的意思。如果您有某种提供对象实例的自定义方法,则可以使用
Provider
绑定和@Provides
方法,并且不需要@Inject< /code> 类本身的注释。
First of all, it's possible to use Guice without putting an
@Inject
annotation anywhere. Guice supports Provider bindings, @Provides methods and constructor bindings, all of which allow you to bind types however you choose. However, for its normal operation it requires@Inject
annotations to serve as metadata telling it what dependencies a class requires and where it can inject them.There reason for this is that otherwise, it cannot deterministically tell what it should inject and where. For example, classes may have multiple constructors and Guice needs some way of choosing one to inject that doesn't rely on any guessing. You could say "well, my classes only have one constructor so it shouldn't need
@Inject
on that", but what happens when someone adds a new constructor to a class? Then Guice no longer has its basis for deciding and the application breaks. Additionally, this all assumes that you're only doing constructor injection. While constructor injection is certainly the best choice in general, Guice allows injection of methods (and fields) as well, and the problem of needing to specify the injection points of a class explicitly is stronger there since most classes will have many methods that are not used for injection and at most a few that are.In addition to
@Inject
's importance in telling Guice, it also serves as documentation of how a class is intended to be used--that the class is part of an application's dependency injection wired infrastructure. It also helps to be consistent in applying@Inject
annotations across your classes, even if it wouldn't currently be absolutely necessary on some that just use a single constructor. I'd also note that you can use JSR-330's@javax.inject.Inject
annotation in Guice 3.0 if a standard Java annotation is preferable to a Guice-specific one to you.I'm not too clear on what you mean by asking the scope for a provider. Scopes generally do not create objects themselves; they control when to ask the unscoped provider of a dependency for a new instance and how to control the scope of that instance. Providers are part of how they operate, of course, but I'm not sure if that's what you mean. If you have some custom way of providing instances of objects,
Provider
bindings and@Provides
methods are the way to go for that and don't require@Inject
annotations on the classes themselves.NO YOU DONT
GUICE 不会要求您注入每个对象。 GUICE 将尝试仅创建注入的对象。所以你可以@Inject你想要注入的对象。
关于范围位 - 范围本质上控制 GUICE 如何创建对象。当您编写自己的自定义范围时,您可以拥有一个控制对象创建方式的数据结构。当您使用自定义注释确定类的范围时,GUICE 将在使用该类的提供程序创建之前调用您的范围方法。然后,您可以决定是否要创建新对象或使用数据结构中的现有对象(例如哈希图或其他内容)。如果你想使用现有的,你可以得到它并返回该对象,否则你可以执行provider.get()并返回。
请注意,这
是一个教程...
http://code.google.com/p /google-guice/wiki/CustomScopes
NO YOU DONT
GUICE does not ask you to inject every single object. GUICE will try and create only injected objects. So you can @Inject objects that you want to be injected.
On the scope bit - Scope essentially controls how your objects gets created by GUICE. When you write your own custom scope you can have a datastructure that controls the way objects are created. When you scope a class with your custom annotation, GUICE will call your scope method before creation with a Provider for that class. You can then decide if you want to create a new object or use an existing object from a datastructure (such as hashmap or something). If you want to use an existing one you get that and return the object, else you do a provider.get() and return.
Notice this
Here's a tutorial ...
http://code.google.com/p/google-guice/wiki/CustomScopes
在最基本的层面上,
@Inject
注释标识了 guice 需要为您设置的内容。您可以将 guice 直接注入到字段、方法或构造函数中。每次您希望 guice 注入对象时,您都必须使用@Inject
注释。这里是一个 guice 教程。
At the most basic level, the
@Inject
annotation identifies the stuff guice will need to set for you. You can have guice inject into a field directly, into a method, or into a constructor. You must use the@Inject
annotation every time you want guice to inject an object.Here is a guice tutorial.