是否可以使用Guice注入请求注入的类? [英] Is it possible to inject the class requesting injection using Guice?

查看:63
本文介绍了是否可以使用Guice注入请求注入的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对象的注入实例知道请求其注入的类的名称。我知道这种违背了依赖注入的整个概念,但它似乎是支持有用日志记录的有效用例。

I'd like an injected instance of an object to know the name of the class that is requesting its injection. I'm aware that this kind of violates the entire concept of dependency injection, but it seems like a valid use case for supporting useful logging. Is this possible with Guice?

示例:

class InjectorAware {
   @Inject
   public InjectorAware(Class injectorClass){
      System.out.println("I was injected into a "+injectorClass.getCanonicalName());
   }
}

class NeedsInjectorAwareField {
   @Inject InjectorAware injectorAware;
}

当NeedsInjectorAwareField被注入时,控制台将打印我被注入到somepackage中.NeedsInjectorAwareField

When NeedsInjectorAwareField gets injected, the console would print "I was injected into a somepackage.NeedsInjectorAwareField"

推荐答案

不可能只使用Guice,而且他们也不允许。

It is not possible using only Guice and they wont allow it.

http://code.google.com / p / google-guice / issues / detail?id = 27

这篇关于是否可以使用Guice注入请求注入的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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