I'm trying to use Guice to inject Log4J Logger instances into classes as described in the Guice documentation:
http://code.google.com/p/google-guice/wiki/CustomInjections
However, as noted in some of the comments on that wiki page, this scheme does not support a constructor injection. So I can't do this:
public class Foo {
@InjectLogger Logger logger;
@Inject
public Foo(<injected parameters>) {
logger.info("this won't work because logger hasn't been injected yet");
...
}
public bar() {
logger.info("this will work because by the time bar() is called,")
logger.info("the logger has been injected");
}
}
Is there another way to handle this injection so that the logger is injected in time for the constructor to use?
Thanks to the help from jfpoilpret, I was able to get the behavior I wanted. I used a conditional in hear() to leverage reflection when the Logger variable is modified as static, otherwise it uses normal Guice field injection.