当前位置: 首页 > 知识库问答 >
问题:

使用factory with assisted parameters创建实例会引发Google Guice异常

濮阳君浩
2023-03-14
public interface JobConfiguration {
    void execute();
}
public class DecryptJobConfiguration implements JobConfiguration {
    @Inject
    public DecryptJobConfiguration(@Assisted("secretKey") String secretKey,
                                   @Assisted("inputImagePath") String inputImagePath,
                                   ImageDecrypter imageDecrypter) {
        // Do stuff
    }

    @Override
    public void execute(){
        // DO stuff
    }
}
public class EncryptJobConfiguration implements JobConfiguration {
    @Inject
    public EncryptJobConfiguration(@Assisted("inputString") String inputString,
                                   @Assisted("secretKey") String secretKey,
                                   @Assisted("inputImagePath") String inputImagePath,
                                   ImageEncrypter imageEncrypter,
        // Do stuff
    }

    @Override
    public void execute() {
        // Do stuff
    }
}
public interface JobConfigurationFactory {
    @Named("encrypt")
    JobConfiguration createEncrypt(@Assisted("inputString") String inputString,
                                   @Assisted("secretKey") String secretKey,
                                   @Assisted("inputImagePath") String inputImagePath);

    @Named("decrypt")
    JobConfiguration createDecrypt(@Assisted("secretKey") String secretKey,
                                   @Assisted("inputImagePath") String inputImagePath);
}
install(new FactoryModuleBuilder()
        .implement(JobConfiguration.class, Names.named("encrypt"), EncryptJobConfiguration.class)
        .implement(JobConfiguration.class, Names.named("decrypt"), DecryptJobConfiguration.class)
        .build(JobConfigurationFactory.class));
@Inject
public CommandLineArgumentValidator(JobConfigurationFactory jobConfigurationFactory){
    this.jobConfigurationFactory = jobConfigurationFactory;
}
jobConfigurationFactory.createEncrypt("A", "B", "C");

在com.google.inject.assistedinject.FactoryProvider2.invoke(FactoryProvider2.java:824)在com.sun.proxy.$proxy12.createEncrypt(未知源)在com.infojolt.imageEncrypt.CommandLinearGumentValidator.ValidateArguments(CommandLinearGumentValidator.java:29)在com.infojolt.imageEncrypt.CommandLinearGumentValidator.java:29)在ODAccessorImpl.Invoke0(本机方法)

这是我第一次使用Google Guice。我不确定我是否误解了它的工作原理?我应该如何创建EncryptJobConfiguration的新实例?

暂时还没有答案

 类似资料:
  • 问题内容: 是否有可能在Java 引用上创建方法引用的原因 ?这样做可能永远是不正确的,但是会导致错误,以后很难找到: 问题答案: 是否有可能在Java 引用上创建方法引用的原因 ? 不是 ,但是Eclipse在这方面显然存在一个错误(编辑:此问题已得到修复)。根据规范,当您使用JDK的工具时,它会失败,并且在线上会出现NPE 。 证明:http://ideone.com/APWXna(或编译和本

  • 我正在使用Spring In Action 3 Action学习Spring MVC,我已经实现了显示用户注册表的基本程序,一旦我们提交表单,它将使用进行验证。 这是我的Spring控制器: 这是我的Spitter类文件: 这是我的编辑。显示给用户注册的jsp文件: 要加载表单,我将访问URL为:,一旦表单被加载,我只需提交表单而无需输入任何详细信息,以便我可以检查我的表单是否得到验证。但是我得到

  • 问题内容: 我在接口抛出异常的地方读了这段代码,但是实现它的类却没有抛出异常或捕获异常,这是为什么呢?在Java中合法或安全吗? 问题答案: 实现和扩展的一般规则是,您可以使新类或接口的限制较少,而不能限制较多。如果您认为将异常作为限制进行处理的要求,则未声明异常的实现的限制性较小。对该接口进行编码的任何人都不会遇到您的课的麻烦。 -斯坦·詹姆斯 作为http://www.coderanch.co

  • 问题内容: 我有一个简单的方法,可将命令打印到屏幕上,扫描用户的输入,然后将其作为字符串返回。如果用户输入无效,它将通知用户并再次询问。该方法运行完美,但是我的讲师提到我们应该始终关闭资源,因此我回过头来添加了close方法,现在无论用户输入什么,每次调用该方法时都会收到NoSuchElementException。这是代码… 例外总是指向用户输入以scan.nextLine()。trim()开头

  • 根据Hibernate文档,在Hibernate抛出异常后使用会话是不安全的。 如果会话抛出异常,包括任何SQLException,立即回滚数据库事务,调用Session.close()并放弃会话实例。某些会话方法不会使会话处于一致状态。Hibernate抛出的任何异常都不能被视为可恢复的。确保通过在finally块中调用close()来关闭会话。 在我的代码中,我正在做批量插入。我正在使用会话。