我有一个有两个实现的接口
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));
在另一个类中,我希望创建一个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;)五
在 com.google.inject.assistedinject.FactoryProvider2.invoke(FactoryProvider2.java:824) at com.sun.proxy.$Proxy 12.createEncrypt(未知来源) at 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
实例?
事实证明,我错过了问题中的重要信息。我正在使用Google Guice 4.0。升级到 v4.2 解决了问题,无需任何其他代码更改。