Is there a way to get Guice to work quickly during Guice.createInjector

My project uses Guice as the IOC container responsible for providing dependencies (classes of service) to a large object schedule (mostly single). Sometimes, if the dependency is not executed at build time, and this dependency is required for many objects, the failure will be repeated again and again, adding exceptions to Guice ProvisionException .

I can understand the rationality of this behavior, because it gives a list of all the errors that arise in order to save problems with fixing a piece of food. However, I would like to disable this feature and "Fail Fast", since a repeated failure in this case is resource-intensive. In addition, a โ€œProvisionExceptionโ€ contains a list of the same exception.

I understand that this behavior is symptomatic (smells) of bad practice in implementation (i.e., resource-intensive creation of an object), but since dependencies are abstractions for which anyone can provide an implementation and plug-in using dependency injection, This is.

What I would like to know: -

Is there a parameter that allows Guice to complete the creation of the injector in 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)); } } 

Two exceptions were thrown in this test asset

 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) { } } 
+4
source share
1 answer

Is there a parameter that allows Guice to complete the creation of the injector in the first exception?

I'm afraid not, it is not.

You will need to continue with code similar to your example. You can always offer this for the Guice team on your Google code page.

+2
source

Source: https://habr.com/ru/post/1432917/


All Articles