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

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

问题描述

有关我自己的教育着想,我想建立一个简单的依赖注入框架,功能类似于谷歌的吉斯的方式做。这样当被装载的类,它美元,数据P $ P-填充注释字段从工厂类。

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.

在那里我被困在是如何pre-填充注释字段我的课竟然没有做任何工作的实际类。换句话说,当被装载一个类时,我需要能够确定是否有任何字段都与特定的注解,并且如果是这样,检索来自工厂类的值。

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.

有一类权进行反思的一些方法被加载在它之前,pre-填充特定的字段​​,然后返回一个类的实例中使用?

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?

我可以延长我所有的课程需要依赖注入的基类,做所有这些工作的,但我想一定有更好的办法,这样我可以简单地使用@注入(或任何注解我决定使用地说,这一领域需要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.

推荐答案

这吉斯接近这个问题的方法是,它只会填充这是本身吉斯 创建一个实例的领域的 1 。喷油器,创建实例后,可以使用反射API来看看类的字段,并检查他们的注释<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/lang/reflect/Field.html#getDeclaredAnnotations%28%29\"><$c$c>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().

这也是为什么,当你希望注入静态字段,你需要使用的原因<一href=\"http://google-guice.google$c$c.com/svn/tags/3.0/javadoc/com/google/inject/Binder.html#requestStaticInjection%28java.lang.Class%3C?%3E...%29\"><$c$c>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.

吉斯并不简单地扫描您的code的注释;所有注射从明确请求递归(例如 requestStaticInjection() Injector.getInstance()等)。现在往往是最初的,明确要求将某些库code已经作出。

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.

例如,如果你使用吉斯-的servlet 你让吉斯通过创建你的servlet实例服务();与()调用。但是,如果你没有这样做,在你的的web.xml ,而不是离开你的servlet配置,吉斯不会注入到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()

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

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