来自匕首讨论@:
我有一个类从对象图中获取一些依赖项,并在运行时从调用者获取其他依赖项.
public class ImageDownloader {
// Get these dependencies from the injector.
private final HttpClient httpClient;
private final ExecutorService executorService;
// Get these from the caller.
private final URL imageUrl;
private final ImageCallback callback;
...
}
Run Code Online (Sandbox Code Playgroud)
我提出了一个解决方案,在那里我定义了一个工厂,
public class ImageDownloader {
...
public static class Factory {
private final HttpClient httpClient;
private final ExecutorService executorService;
@Inject
public Factory(HttpClient httpClient, ExecutorService executorService) {
this.httpclient = httpClient;
this.executorService = executorService;
}
public ImageDownloader create(URL imageUrl, ImageCallback callback) {
return …Run Code Online (Sandbox Code Playgroud) 使用Google Guice或Gin我可以指定参数不受依赖注入框架控制:
class SomeEditor {
@Inject
public SomeEditor(SomeClassA a, @Assisted("stage") SomeClassB b) {
}
}
Run Code Online (Sandbox Code Playgroud)
辅助参数stage在SomeEditor创建实例时指定.
SomeClassA的实例取自对象图,SomeClassB的实例在运行时从调用者获取.
在Dagger中有类似的方法吗?