Guice 辅助注入更深入地了解依赖关系层次结构
我想进行一系列处理元素,并通过Guice将它们连接在一起。我们假设路径如下:
-
interface A实现方式需要一些输入class AImpl -
interface B按需求实施class BImplA -
interface C按需求实施class CImplB -
interface D按需求实施class DImplC
A 的依赖关系只能在运行时解析,而不能在配置时解析。通常的方法是在这种情况下使用辅助注入来创建一个工厂,该工厂将缺少的实例作为参数,就像这样:
public interface AFactory {
public A createA(String input);
}
但我真正想要的是这样的东西:
public interface DFactory {
public D createD(String inputForA);
}
我不想在整个层次结构中手动传递特定于依赖项。有没有可能用Guice实现这一点?如果没有,那么在保持注射益处的同时优雅地规避这个问题的最佳方法是什么?AImpl