使用带有辅助参数的工厂创建实例会引发 Google Guice 异常
Creating an instance using factory with assisted parameters throws exception with Google Guice
我有一个接口有两个实现
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
实例的另一个 class 中,我注入 JobConfigurationFactory
:
@Inject
public CommandLineArgumentValidator(JobConfigurationFactory jobConfigurationFactory){
this.jobConfigurationFactory = jobConfigurationFactory;
}
后来在我调用的一种方法中 createEncrypt
:
jobConfigurationFactory.createEncrypt("A", "B", "C");
我希望这 return 我是 EncryptJobConfiguration
的一个实例,但它导致了这个异常:
java.lang.NoSuchMethodError:
com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V
at
com.google.inject.assistedinject.FactoryProvider2.invoke(FactoryProvider2.java:824)
at com.sun.proxy.$Proxy12.createEncrypt(Unknown Source) at
com.infojolt.imageencrypt.CommandLineArgumentValidator.validateArguments(CommandLineArgumentValidator.java:29)
at
com.infojolt.imageencrypt.CommandLineArgumentValidatorTest.validateArgumentsReturnsInputStringWhenAllRequiredFieldsAreSet(CommandLineArgumentValidatorTest.java:55)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method)
这是我第一次使用 Google Guice,我不确定我是否误解了它应该如何工作?我应该如何创建 EncryptJobConfiguration
的新实例?
原来我漏掉了题中的一个重要信息。我使用的是 Google Guice 4.0。升级到 v4.2 解决了这个问题,没有任何其他代码更改。
我有一个接口有两个实现
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
实例的另一个 class 中,我注入 JobConfigurationFactory
:
@Inject
public CommandLineArgumentValidator(JobConfigurationFactory jobConfigurationFactory){
this.jobConfigurationFactory = jobConfigurationFactory;
}
后来在我调用的一种方法中 createEncrypt
:
jobConfigurationFactory.createEncrypt("A", "B", "C");
我希望这 return 我是 EncryptJobConfiguration
的一个实例,但它导致了这个异常:
java.lang.NoSuchMethodError: com.google.common.base.Preconditions.checkState(ZLjava/lang/String;Ljava/lang/Object;)V
at com.google.inject.assistedinject.FactoryProvider2.invoke(FactoryProvider2.java:824) at com.sun.proxy.$Proxy12.createEncrypt(Unknown Source) at com.infojolt.imageencrypt.CommandLineArgumentValidator.validateArguments(CommandLineArgumentValidator.java:29) at com.infojolt.imageencrypt.CommandLineArgumentValidatorTest.validateArgumentsReturnsInputStringWhenAllRequiredFieldsAreSet(CommandLineArgumentValidatorTest.java:55) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
这是我第一次使用 Google Guice,我不确定我是否误解了它应该如何工作?我应该如何创建 EncryptJobConfiguration
的新实例?
原来我漏掉了题中的一个重要信息。我使用的是 Google Guice 4.0。升级到 v4.2 解决了这个问题,没有任何其他代码更改。