Guice 如何填充注释字段 [英] How does Guice Populate Annotated Fields

查看:32
本文介绍了Guice 如何填充注释字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了我自己的教育,我想构建一个简单的依赖注入框架,其功能类似于 Google 的 Guice 所做的方式.因此,当一个类被加载时,它会用来自工厂类的数据预先填充带注释的字段.

For the sake of my own education, I wanted to build a simple Dependency Injection framework that functions similar to the way Google's Guice does. So that when a class is loaded, it pre-populates annotated fields with data from a factory class.

我在编译时使用反射来扫描我所有的工厂类并将这些类保存在一个静态列表中,以便在加载我的类时,我可以引用我的工厂,然后我可以扫描方法并返回适当的数据.

I am using Reflections to scan all my factory classes at compile time and save those classes in a static list so that when it comes time to load my classes, I have a reference to my factories that I can then scan methods and return the appropriate data.

我遇到的问题是如何预先填充我的类注释字段,而无需在实际类中实际执行任何工作.换句话说,当一个类被加载时,我需要能够确定是否有任何字段用特定的注释进行了注释,如果是,则从工厂类中检索值.

Where i'm stuck at is how to pre-populate my classes annotated fields without actually doing any of the work in the actual class. In other words, when a class is loaded, I need to be able to determine if any of the fields are annotated with a specific annotation, and if they are, retrieve the value from the factory class.

是否有某种方法可以在类加载之前对其进行反射,预先填充特定字段,然后返回要使用的该类的实例?

Is there some way of performing reflection on a class right before it is loaded, pre-populate specific fields and then return an instance of that class to be used?

我可以使用一个完成所有这些工作的基类来扩展我所有需要依赖注入的类,但我认为必须有更好的方法,以便我可以简单地使用 @Inject(或我决定使用的任何注释)过去常说这个领域需要 DI) 并且神奇地"完成了所有工作.

I could extend all of my classes that require dependency injection with a base class that does all of this work, but I figure there must be a better way so that I can simply use an @Inject (or whatever annotation I decide to use to say that this field requires DI) and "magically" all the work is done.

推荐答案

Guice 解决这个问题的方式是它只会填充由 Guice 本身创建的实例的字段1.在创建实例后,注入器可以使用反射 API 查看 Class 的字段并使用 Field.getDeclaredAnnotations().

The way that Guice approaches this is that it will only populate the fields of an instance that was itself created by Guice1. The injector, after creating the instance, can use the Reflection API to look at the fields of the Class and inspect their annotations with Field.getDeclaredAnnotations().

这也是为什么,当你想注入静态字段时,你需要使用 Binder.requestStaticInjection() 填充静态字段.

This is also the reason why, when you want to inject into a static field, you need to use Binder.requestStaticInjection() to populate the static fields.

Guice 不会简单地扫描您的代码以获取注释;所有注入都从显式请求中递归(例如 requestStaticInjection()Injector.getInstance() 等).现在,最初的、明确的请求通常是在某些库代码中提出的.

Guice does not simply scan your code for annotations; all injections recurse from an explicit request (e.g. requestStaticInjection(), Injector.getInstance(), etc). Now often that initial, explicit request will have been made in some library code.

例如,如果您使用 guice-servlet,您可以让 Guice 通过使用 serve().with() 调用来创建 servlet 的实例.但是,如果您不这样做,而是将您的 servlet 配置留在您的 web.xml 中,Guice 将不会注入您的 servlet.

For example, if you're using guice-servlet you let Guice create the instances of your servlet by using the serve().with() calls. But if you didn't do that, and instead left your servlet config in your web.xml, Guice would not inject into your servlet.

1 - 您还可以使用 Binder.requestInjection() 请求显式注入.

1 - You can also request explicit injection using Binder.requestInjection().

这篇关于Guice 如何填充注释字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆