【问题标题】:Creating an instance using factory with assisted parameters throws exception with Google Guice使用带有辅助参数的工厂创建实例会引发 Google Guice 异常
【发布时间】:2018-04-17 15:20:44
【问题描述】:

我有两个实现的接口

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

安装在 Google Guice 中:

install(new FactoryModuleBuilder()
        .implement(JobConfiguration.class, Names.named("encrypt"), EncryptJobConfiguration.class)
        .implement(JobConfiguration.class, Names.named("decrypt"), DecryptJobConfiguration.class)
        .build(JobConfigurationFactory.class));

在我希望创建EncryptJobConfiguration 实例的另一个类中,我注入JobConfigurationFactory

@Inject
public CommandLineArgumentValidator(JobConfigurationFactory jobConfigurationFactory){
    this.jobConfigurationFactory = jobConfigurationFactory;
}

后来在我称为createEncrypt的一种方法中:

jobConfigurationFactory.createEncrypt("A", "B", "C");

我希望这会返回一个 EncryptJobConfiguration 的实例,但它会导致此异常:

java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V

在 com.google.inject.assistedinject.FactoryProvider2.invoke(FactoryProvider2.java:824) 在 com.sun.proxy.$Proxy12.createEncrypt(Unknown Source) 在 com.infojolt.imageencrypt.CommandLineArgumentValidator.validateArguments(CommandLineArgumentValidator.java:29) 在 com.infojolt.imageencrypt.CommandLineArgumentValidatorTest.validateArgumentsReturnsInputStringWhenAllRequiredFieldsAreSet(CommandLineArgumentValidatorTest.java:55) 在 java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native 方法)

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

【问题讨论】:

    标签: java dependency-injection guice guice-3


    【解决方案1】:

    原来我错过了问题中的一条重要信息。我使用的是 Google Guice 4.0。升级到 v4.2 解决了这个问题,无需任何其他代码更改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多