Guice Custom Injection for constructor parameters

I read about Custom Injection in Guice here: https://github.com/google/guice/wiki/CustomInjections

This scheme describes how you can create a MembersInjector for injecting fields of a class. I am able to do this successfully. However, I do not like the idea of field injection and would like to do this to inject parameters of a constructor.

Does anybody have a clue as to how this can be achieved for constructor injection?

Specifically:

public class PaymentService {
  private final Logger logger;

  @Inject public PaymentService(@InjectLogger(type="log4j") final Logger logger){
       //the @InjectLogger is the injection point
  }


  ...
}

The documentation of MembersInjector says "inject dependencies into fields and methods of type T": http://google.github.io/guice/api-docs/latest/javadoc/index.html?com/google/inject/MembersInjector.html

This seems to indicate that it should be possible to do this. Any ideas?

Also, to clarify, I need to use custom injection because I don't have the binding at development time. I have to bind a logger at runtime based on the attribute in my annotation from a configuration. I also cannot iterate through all my config keys.

链接地址: http://www.djcxy.com/p/94650.html

上一篇: 在Dropwizard应用程序中的Guice AbstractModule中注入配置

下一篇: Guice自定义注入构造函数参数