在 Google Guice 中将构造函数参数传递给工厂
Pass constructor parameter into factory in Google Guice
我正在尝试将构造函数参数从一个 class 传递到另一个工厂 class:
public class PerPixelImageUpdater implements ImageUpdater {
private final PixelUpdater pixelUpdater;
private final BufferedImage image;
@Inject
PerPixelImageUpdater(PixelUpdaterFactory pixelUpdaterFactory,
@Assisted BufferedImage image){
this.image = image;
this.pixelUpdater = pixelUpdaterFactory.create(image);
}
//Methods
}
工厂:
public interface ImageUpdaterFactory {
ImageUpdater create(BufferedImage image);
}
在 Guice 中注册:
install(new FactoryModuleBuilder()
.implement(ImageUpdater.class, PerPixelImageUpdater.class)
.build(ImageUpdaterFactory.class));
当我 运行 这段代码时,我得到一个错误,说实现没有绑定?
1) No implementation for java.awt.image.BufferedImage annotated with
@com.google.inject.assistedinject.Assisted(value="") was bound.
while locating java.awt.image.BufferedImage annotated with
@com.google.inject.assistedinject.Assisted(value="")
for the 2nd parameter of com.infojolt.imageencrypt.PerPixelImageUpdater.(PerPixelImageUpdater.java:16)
我错过了什么?我需要以不同的方式注册工厂吗?
额外的细节:
实际上有两个错误
1) No implementation for java.awt.image.BufferedImage annotated with
@com.google.inject.assistedinject.Assisted(value="") was bound.
while locating java.awt.image.BufferedImage annotated with
@com.google.inject.assistedinject.Assisted(value="")
for the 2nd parameter of com.infojolt.imageencrypt.PerPixelImageUpdater.(PerPixelImageUpdater.java:16)
at
com.infojolt.imageencrypt.Injection.ImageEncryptModule.configure(ImageEncryptModule.java:25)
2) No implementation for java.awt.image.BufferedImage annotated with
@com.google.inject.assistedinject.Assisted(value="") was bound.
while locating java.awt.image.BufferedImage annotated with
@com.google.inject.assistedinject.Assisted(value="")
for the 3rd parameter of com.infojolt.imageencrypt.SimplePixelUpdater.(SimplePixelUpdater.java:17)
at
com.infojolt.imageencrypt.Injection.ImageEncryptModule.configure(ImageEncryptModule.java:18)
在PixelShiftImageEncrypter
中,ImageUpdaterFactory imageUpdaterFactory
传入构造函数:
@Inject
PixelShiftImageEncrypter(PixelShiftCalculator pixelShiftCalculator,
ImageLoader imageLoader,
StringEncryption stringEncryption,
PixelModificationCalculator pixelModificationCalculator,
PixelSkipCountCalculator pixelSkipCountCalculator,
ImageUpdaterFactory imageUpdaterFactory)
(我目前正在重构这个项目以减少构造函数参数/方法参数的数量,但我试图在将其拆开之前让 classed 处于测试状态。)
ImageUpdater
然后通过调用以下方法在方法中创建:
ImageUpdater imageUpdater = imageUpdaterFactory.create(image);
PixelShiftImageEncrypter
创建于:
@Inject
EncryptJobConfiguration(@Assisted("inputString") String inputString,
@Assisted("secretKey") String secretKey,
@Assisted("inputImagePath") String inputImagePath,
ImageEncrypter imageEncrypter,
ImageStorage imageStorage)
ImageEncryptModule.java
包含:
install(new FactoryModuleBuilder()
.implement(JobConfiguration.class, Names.named("encrypt"), EncryptJobConfiguration.class)
.implement(JobConfiguration.class, Names.named("decrypt"), DecryptJobConfiguration.class)
.build(JobConfigurationFactory.class));
install(new FactoryModuleBuilder()
.implement(PixelUpdater.class, SimplePixelUpdater.class)
.build(PixelUpdaterFactory.class));
install(new FactoryModuleBuilder()
.implement(ImageUpdater.class, PerPixelImageUpdater.class)
.build(ImageUpdaterFactory.class));
PixelUpdaterFactory
:
public interface PixelUpdaterFactory {
PixelUpdater create(BufferedImage image);
}
JobConfigurationFactory
:
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);
}
此文件还包含我为简洁起见排除的所有其他绑定。例如:
bind(ImageEncrypter.class).to(PixelShiftImageEncrypter.class);
问题是我以为我已经替换了:
bind(ImageUpdater.class).to(PerPixelImageUpdater.class);
与:
install(new FactoryModuleBuilder()
.implement(ImageUpdater.class, PerPixelImageUpdater.class)
.build(ImageUpdaterFactory.class));
但是,原来的绑定并没有被移除。删除这两行解决了问题:
bind(ImageUpdater.class).to(PerPixelImageUpdater.class);
bind(PixelUpdater.class).to(SimplePixelUpdater.class);
我正在尝试将构造函数参数从一个 class 传递到另一个工厂 class:
public class PerPixelImageUpdater implements ImageUpdater {
private final PixelUpdater pixelUpdater;
private final BufferedImage image;
@Inject
PerPixelImageUpdater(PixelUpdaterFactory pixelUpdaterFactory,
@Assisted BufferedImage image){
this.image = image;
this.pixelUpdater = pixelUpdaterFactory.create(image);
}
//Methods
}
工厂:
public interface ImageUpdaterFactory {
ImageUpdater create(BufferedImage image);
}
在 Guice 中注册:
install(new FactoryModuleBuilder()
.implement(ImageUpdater.class, PerPixelImageUpdater.class)
.build(ImageUpdaterFactory.class));
当我 运行 这段代码时,我得到一个错误,说实现没有绑定?
1) No implementation for java.awt.image.BufferedImage annotated with @com.google.inject.assistedinject.Assisted(value="") was bound.
while locating java.awt.image.BufferedImage annotated with @com.google.inject.assistedinject.Assisted(value="") for the 2nd parameter of com.infojolt.imageencrypt.PerPixelImageUpdater.(PerPixelImageUpdater.java:16)
我错过了什么?我需要以不同的方式注册工厂吗?
额外的细节:
实际上有两个错误
1) No implementation for java.awt.image.BufferedImage annotated with @com.google.inject.assistedinject.Assisted(value="") was bound.
while locating java.awt.image.BufferedImage annotated with @com.google.inject.assistedinject.Assisted(value="") for the 2nd parameter of com.infojolt.imageencrypt.PerPixelImageUpdater.(PerPixelImageUpdater.java:16) at com.infojolt.imageencrypt.Injection.ImageEncryptModule.configure(ImageEncryptModule.java:25)2) No implementation for java.awt.image.BufferedImage annotated with @com.google.inject.assistedinject.Assisted(value="") was bound.
while locating java.awt.image.BufferedImage annotated with @com.google.inject.assistedinject.Assisted(value="") for the 3rd parameter of com.infojolt.imageencrypt.SimplePixelUpdater.(SimplePixelUpdater.java:17) at com.infojolt.imageencrypt.Injection.ImageEncryptModule.configure(ImageEncryptModule.java:18)
在PixelShiftImageEncrypter
中,ImageUpdaterFactory imageUpdaterFactory
传入构造函数:
@Inject
PixelShiftImageEncrypter(PixelShiftCalculator pixelShiftCalculator,
ImageLoader imageLoader,
StringEncryption stringEncryption,
PixelModificationCalculator pixelModificationCalculator,
PixelSkipCountCalculator pixelSkipCountCalculator,
ImageUpdaterFactory imageUpdaterFactory)
(我目前正在重构这个项目以减少构造函数参数/方法参数的数量,但我试图在将其拆开之前让 classed 处于测试状态。)
ImageUpdater
然后通过调用以下方法在方法中创建:
ImageUpdater imageUpdater = imageUpdaterFactory.create(image);
PixelShiftImageEncrypter
创建于:
@Inject
EncryptJobConfiguration(@Assisted("inputString") String inputString,
@Assisted("secretKey") String secretKey,
@Assisted("inputImagePath") String inputImagePath,
ImageEncrypter imageEncrypter,
ImageStorage imageStorage)
ImageEncryptModule.java
包含:
install(new FactoryModuleBuilder()
.implement(JobConfiguration.class, Names.named("encrypt"), EncryptJobConfiguration.class)
.implement(JobConfiguration.class, Names.named("decrypt"), DecryptJobConfiguration.class)
.build(JobConfigurationFactory.class));
install(new FactoryModuleBuilder()
.implement(PixelUpdater.class, SimplePixelUpdater.class)
.build(PixelUpdaterFactory.class));
install(new FactoryModuleBuilder()
.implement(ImageUpdater.class, PerPixelImageUpdater.class)
.build(ImageUpdaterFactory.class));
PixelUpdaterFactory
:
public interface PixelUpdaterFactory {
PixelUpdater create(BufferedImage image);
}
JobConfigurationFactory
:
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);
}
此文件还包含我为简洁起见排除的所有其他绑定。例如:
bind(ImageEncrypter.class).to(PixelShiftImageEncrypter.class);
问题是我以为我已经替换了:
bind(ImageUpdater.class).to(PerPixelImageUpdater.class);
与:
install(new FactoryModuleBuilder()
.implement(ImageUpdater.class, PerPixelImageUpdater.class)
.build(ImageUpdaterFactory.class));
但是,原来的绑定并没有被移除。删除这两行解决了问题:
bind(ImageUpdater.class).to(PerPixelImageUpdater.class);
bind(PixelUpdater.class).to(SimplePixelUpdater.class);