Search code examples
javaguiceanti-patterns

How could I rewrite this code snippet so that I'm not manually/explicitly invoking the injector?


After reading through many code examples for Google Guice, I see a lot of code that looks like this:

public class MyModule implements Module {
    public void configure(Binder binder) {
        binder.bind(Service.class).to(ServiceImpl.class);
    }
}

public class Widget {
    private Injector injector = Guice.createInjector(new MyModule());

    public void doSomething() {
        Service service = injector.getInstance(Service.class);
        service.run();
    }
}

It is my understanding that code which explicitly calls the IoC mechanism (such as injector.getInstance(Class<T>) in Guice or appContext.getBean(String) in Spring) is indeed an IoC anti-pattern.

In these examples I notice there is no mention of the @Inject or @ImplementedBy annotations, which I believe are Guice's workarounds to this anti-pattern.

How could I rewrite the code snippet above so that I'm not manually/explicitly invoking the injector? I'd like everything configured so that by the time execution gets to the doSomething() method, the application already "knows" what Service implementation we'll be using...

Thanks in advance!


Solution

  • Make Guice aware of your Widget by adding it to a module and injecting it when needed using annotations.

    You will need to add Service to its constructor:

    public class Widget {
    
        private final Service service;
    
        @Inject
        public Widget(Service service) {
            this.service = service;
        }
    }