Please don't do this - you're using a single static logger behind an
instance variable, and using Logger quite differently than it's
intended. And what happens if someone doens't force the
constructor... log is not initialized. Or what if there are multiple
injectors in the VM - now you have one instance of Logger taking
precedence over the other.
You should just let Utils be a normal class and have normal instances
created. It works wonderfully. Games with statics are quite
dangerous like that - especially statics that depend on state derived
from instances' behaviour.
Christian.
On Apr 27, 2010, at 7:51 AM, Jonathan Abourbih wrote:
I'm also a bit new to Guice, but I'll give it a shot. One way to
avoid the static injection would be to use an instance of the Utils
class:
@Singleton
public class Utils
{
private static Logger log;
@Inject
private TestStatic(Foo myFoo) {
TestStatic.myFoo = myFoo;
}
public static void logFoo(Foo bar) {
log.info(bar.toString());
}
}
And when you initialise your injector, just include:
Injector injector = Guice.createInjector( new AbstractModule() {
@Override
protected void configure()
{
bind(Logger.class).to(ReallyCoolLogImplementation.class);
bind(Utils.class);
}});
injector.getInstance(Utils.class); //force Guice to call the
constructor
Now, when you use the Utils class anywhere else in your code, you
can just call Utils.logFoo(myFoo). This seems like an awful lot of
work though.
Jonathan
On 27 Apr 2010, at 10:44, Patrick Bergner 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. 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
.
--
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.