Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get Guice to fail fast during Guice.createInjector

My project is using Guice as the IOC container responsible for providing dependencies (service classes) to a large graph of objects (mostly singletons). Sometimes if a dependency fails during construction and this dependency is required by many objects, the failure will occur over and over again adding the exceptions to the Guice ProvisionException.

I can understand the rational for this behaviour as it gives a list of all the errors that occur to save fixing issues piece meal. However, I would like to disable this feature and 'Fail Fast', as the repeated failure in this case is resource intensive. Further more the 'ProvisionException' contains a list of the same exception.

I do appreciate that this behaviour is symptomatic (smells) of bad practice in the implementation (i.e. resource intensive object creation), but since the dependencies are abstractions for which anyone can provide implementations and plugin using dependency injection there is little defence against it.

What I would like to know is:-

Is there a parameter that enables Guice to exit Injector creation at the first exception?

Any help would be greatly appreciated.

Edit:

@Test
    public void guiceExample()
    {
        Injector injector = Guice.createInjector(new TestModule());
        try{
        IAmANeedyObject instance = injector.getInstance(IAmANeedyObject.class);
        }
        catch (ProvisionException e)
        {
            assertThat(e.getErrorMessages().size(),Is.is(2));
        }
    } 

This test assets two exceptions have been thrown

import com.google.inject.AbstractModule;
import com.google.inject.Inject;

public class TestModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(IWasDesignedWithHonestIntent.class).to(NastyThrowingExample.class);
        bind(IMindMyOwnBusiness.class).to(SomeLucklessObject.class);
        bind(IAlsoMindMyOwnBusiness.class).to(SomeEquallyLucklessObject.class);
        bind(IAmANeedyObject.class).to(LowSelfEsteem.class);
    }
}

interface IWasDesignedWithHonestIntent {}

interface IMindMyOwnBusiness {}

interface IAlsoMindMyOwnBusiness {}

interface IAmANeedyObject {}

@Singleton
class NastyThrowingExample implements IWasDesignedWithHonestIntent {
    @Inject
    public NastyThrowingExample() throws LongSlowAgonisingDeathException {
        throw new LongSlowAgonisingDeathException("I am dying");
    }
}

class LongSlowAgonisingDeathException extends Exception {
    @Inject
    public LongSlowAgonisingDeathException(String message) {
        super(message);
    }
}

class SomeLucklessObject implements IMindMyOwnBusiness {
    @Inject
    public SomeLucklessObject(IWasDesignedWithHonestIntent designedWithHonestIntent) {
    }
}

class SomeEquallyLucklessObject implements IAlsoMindMyOwnBusiness {
    @Inject
    public SomeEquallyLucklessObject(IWasDesignedWithHonestIntent designedWithHonestIntent) {
    }
}

class LowSelfEsteem implements IAmANeedyObject {
    @Inject
    public LowSelfEsteem(IMindMyOwnBusiness iMindMyOwnBusiness, IAlsoMindMyOwnBusiness alsoMindMyOwnBusiness) {
    }
}
like image 421
Yoztastic Avatar asked Sep 07 '12 09:09

Yoztastic


People also ask

Is dagger faster than Guice?

Supposedly faster than Guice, because Dagger works by generating code up-front, whereas Guice uses Reflection at runtime.

How does @inject work Guice?

Using GuiceIn each of your constructors that need to have something injected in them, you just add an @Inject annotation and that tells Guice to do it's thing. Guice figures out how to give you an Emailer based on the type. If it's a simple object, it'll instantiate it and pass it in.

What is binding in Guice?

A binding is an object that corresponds to an entry in the Guice map. You add new entries into the Guice map by creating bindings.

How do you test a Guice?

Typically the best way to test Guice modules is to just create an injector in your test and ensure you can get instances of keys you care about out of it. To do this without causing production stuff to happen you may need to replace some modules with other modules. You can use Modules.


1 Answers

Is there a parameter that enables Guice to exit Injector creation at the first exception?

I'm afraid that don't, it doesn't.

You will have to continue with a code like your example. You can always suggest this for the Guice team on their Google Code page.

like image 76
caarlos0 Avatar answered Oct 20 '22 20:10

caarlos0