Guice - Inject dependency into a class with static helper methods
Asked Answered
S

1

8

I'm still new to Guice and haven't used any DI frameworks before. After reading the official wiki and many other documents I'm still unable to wrap my head around it completely.

In my particular case I want to write a EL taglib function that uses some other (to-be-injected) class. As all taglib functions have to be declared as static, I can't just @Inject my dependency via constructor or setter. I thought of using the requestStaticInjection() method described in http://code.google.com/p/google-guice/wiki/Injections#Static_Injections but I unable to get it to work and haven't been able to find any good tutorial.

Thanks in advance for any help,

Arman

Scofflaw answered 26/8, 2013 at 23:44 Comment(1)
"I unable to get it to work" So what have you tried?Cowling
E
20

It doesn't get much more clear than that Guice documentation but here's a unit test that shows an example of how you can use static injection:

public class StaticInjectionExample {

  static class SomeClass {}

  static class TagLib{
    @Inject
    static SomeClass injected;

    public static void taglibFunction(String foo) {
      injected.something(foo);
    }

  }

  static class TestModule extends AbstractModule {
    @Override
    protected void configure() {
      requestStaticInjection(TabLib.class);
    }
  }

  @Test
  public void test() {
    Injector injector = Guice.createInjector(new TestModule());
    TagLib receiver = injector.getInstance(TagLib.class);
    // Do something with receiver.injected
  }
}
Epispastic answered 27/8, 2013 at 0:14 Comment(9)
Thanks condit for your response. I'll try it you. But will this honor any depedency injected by other Injectors? For instance, will Guice use the same instance if the injected class has been marked as @Singleton?Scofflaw
I'm not sure what you mean by "other injectors". If you bind Injected as @Singleton then that single instance will be used during injection (including static injection) for the entire module.Epispastic
According to the Guice documentation, you'd normally create an Injector within a web project inside of a ServletContextListener extending GuiceServletContextListener. So if I create an additional Injector inside of some class, will Guice inject the same @Singleton class as the "ServletContextListener-Injector" would have?Scofflaw
I'm not sure why you'd need to create that additional injector. Is there some reason you don't want to install that module when you're configuring your injector in the GuiceServletContextListener?Epispastic
I do configure the injector in the GuiceServletContextListener. But how do I get access to injector from another class in a static method? I solved my problem by declaring the injector in GuiceServletContextListener as public static final and just access it that way. But I assume this is hardly good style :/Scofflaw
Why do you need to get the injector? Can't you just @Inject an instance of what you need in your class?Epispastic
Have a look to following sample class public class TagLib { public static void taglibFunction(String foo) { // does something but needs a instance from some class that has to be injected via guice } }. I can't @Inject an instance into the taglibFunction method.Scofflaw
I updated the answer to match these class names. You could request static injection on TagLib and then inject TagLib wherever you need it. Guice will handle the rest.Epispastic
Fortunately, I was able to get rid of any taglibFunctions in the project. But if I needed an instance from a injector, your solution would work. Thanks for your support :)Scofflaw

© 2022 - 2024 — McMap. All rights reserved.