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]<google-guice%[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.

Reply via email to