我想进行一系列处理元素并通过 Guice 将它们连接在一起。让我们假设以下路径:
interface A
class AImpl
需要一些输入来实现interface B
按class BImpl
需要执行A
interface C
按class CImpl
需要执行B
interface D
按class DImpl
需要执行C
A 的依赖关系只能在运行时解决,而不能在配置时解决。通常的方法是在这种情况下使用辅助注入来创建一个工厂,它将丢失的实例作为参数,就像这样:
public interface AFactory {
public A createA(String input);
}
但我真正想要的是这样的:
public interface DFactory {
public D createD(String inputForA);
}
我不想AImpl
在整个层次结构中手动传递特定的依赖项。是否有可能使用 Guice 实现这一目标?如果不是,那么在保持注入优势的同时优雅地规避这个问题的最佳方法是什么?