jjzjj

java - 有没有办法让 Guice 在 Guice.createInjector 期间快速失败

coder 2024-04-02 原文

我的项目正在使用 Guice作为负责为大型对象图(主要是单例)提供依赖项(服务类)的 IOC 容器。有时,如果在构造过程中依赖项失败,并且许多对象都需要此依赖项,则失败将一遍又一遍地发生,并将异常添加到 Guice ProvisionException 中。

我能理解这种行为的合理性,因为它列出了所有发生的错误,以节省修复问题的时间。但是,我想禁用此功能和“快速失败”,因为在这种情况下反复失败是资源密集型的。此外,“ProvisionException”包含相同异常的列表。

我很欣赏这种行为是实现中不良实践(即资源密集型对象创建)的症状(气味),但由于依赖项是抽象的,任何人都可以使用依赖项注入(inject)为其提供实现和插件,因此几乎没有防御措施它。

我想知道的是:-

是否有一个参数可以让 Guice 在第一次出现异常时退出 Injector 创建?

如有任何帮助,我们将不胜感激。

编辑:

@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));
        }
    } 

此测试 Assets 已抛出两个异常

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) {
    }
}

最佳答案

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

恐怕没有,没有。

您将不得不继续使用您的示例中的代码。 您可以随时在他们的 Google 代码页上向 Guice 团队提出建议。

关于java - 有没有办法让 Guice 在 Guice.createInjector 期间快速失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12315877/

有关java - 有没有办法让 Guice 在 Guice.createInjector 期间快速失败的更多相关文章

  1. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  2. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  3. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  4. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  5. ruby - 即使失败也继续进行多主机测试 - 2

    我已经构建了一些serverspec代码来在多个主机上运行一组测试。问题是当任何测试失败时,测试会在当前主机停止。即使测试失败,我也希望它继续在所有主机上运行。Rakefile:namespace:specdotask:all=>hosts.map{|h|'spec:'+h.split('.')[0]}hosts.eachdo|host|begindesc"Runserverspecto#{host}"RSpec::Core::RakeTask.new(host)do|t|ENV['TARGET_HOST']=hostt.pattern="spec/cfengine3/*_spec.r

  6. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  7. 没有类的 Ruby 方法? - 2

    大家好!我想知道Ruby中未使用语法ClassName.method_name调用的方法是如何工作的。我头脑中的一些是puts、print、gets、chomp。可以在不使用点运算符的情况下调用这些方法。为什么是这样?他们来自哪里?我怎样才能看到这些方法的完整列表? 最佳答案 Kernel中的所有方法都可用于Object类的所有对象或从Object派生的任何类。您可以使用Kernel.instance_methods列出它们。 关于没有类的Ruby方法?,我们在StackOverflow

  8. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  9. ruby-on-rails - 有没有办法为 CarrierWave/Fog 设置上传进度指示器? - 2

    我在Rails应用程序中使用CarrierWave/Fog将视频上传到AmazonS3。有没有办法判断上传的进度,让我可以显示上传进度如何? 最佳答案 CarrierWave和Fog本身没有这种功能;你需要一个前端uploader来显示进度。当我不得不解决这个问题时,我使用了jQueryfileupload因为我的堆栈中已经有jQuery。甚至还有apostonCarrierWaveintegration因此您只需按照那里的说明操作即可获得适用于您的应用的进度条。 关于ruby-on-r

  10. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

随机推荐