Yes, I would make your methods non-static, make the class instantiatable - if you MUST, make a singleton static factory method, but in your Guice-ified code, don't use the factory method - just inject it where you need it, and in its constructor, ask for the logger. If you use it outside of Guice, then crete the Logger and pass it in to the constructor in the factory method.

public class MyUtils {
  private final Logger logger;
  public MyUtils(Logger logger) {
    this.logger = logger;
  }

  ... code ...

  //
  // STATIC SINGLETON INITIALIZER - DON'T USE THIS IF YOU
  // CAN USE GUICE INSTEAD
  //
private static final MyUtils instance = new MyUtils(Logger.getLogger(MyUtils.class.getName());
  public static MyUtils getInstance() {
    return instance;
  }
  // or some variation of the above - lazily created if necessary
}

I'd even avoid using this, because creating new MyUtils().doSomething() is really not that costly at run-time (if you're not bothering to use guice). It may not even make it into the heap if you don't let a reference to it escape so garbage collection is cheap-o.

Christian.

On Apr 27, 2010, at 6:22 AM, Stuart McCulloch wrote:

On 27 April 2010 17:44, Patrick Bergner <[email protected]> wrote:
Hi, I'm new to Guice and lack experience in using it the suggested
(intended) way. I already figured out most of what I need to use Guice
in a relatively simple application and like it very much but one thing
is missing: injecting static dependencies.

I need this in classes that contain only static methods (a utility
class). For the sake of an example, think of a logger that should be
injected and be usable by the static methods. Let's say it looks like
this:

public class Utils {

 @Inject private static Logger log;

 public static void logFoo( Foo bar )
 {
   log.info( bar.toString() );
 }

}

How do I inject the Logger correctly? My current approach is to use
"requestStaticInjection( Utils.class )" in my Guice module but,
according to the docs, this is discouraged.

It's the use of static dependencies that's discouraged rather than the
requestStaticInjection method itself. If you really need to inject static
dependencies then requestStaticInjection() is the way to go, but you
should also consider ways to avoid these static dependencies in the
first place (if possible).

What other options do I have?

Thanks for your help!

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected] . For more options, visit this group at http://groups.google.com/group/google-guice?hl=en .


--
Cheers, Stuart

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to [email protected] . For more options, visit this group at http://groups.google.com/group/google-guice?hl=en .

--
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to