gpt4 book ai didi

java - 用 Dagger 改造类的 HashMap

转载 作者:行者123 更新时间:2023-12-02 05:35:21 26 4
gpt4 key购买 nike

我对依赖注入(inject)有点陌生,我有疑问。在我的应用程序中,我有一个 HashMap 来存储用于 RetroFit 的构建类(如缓存),但现在我正在使用 Dagger 转向 DI,我想知道如何实现相同的行为。

我的代码:

private Map<String, Object> restInstances;
public <T> T getRestClient(Class<T> clazz) {
T client = null;

if ((client = (T) restInstances.get(clazz.getCanonicalName())) != null) {
return client;
}

client = restAdapter.create(clazz);
restInstances.put(clazz.getCanonicalName(), client);
return client;
}

开始使用 DI 后,我的“模块”类:

 @Provides @Singleton
public JobManager providesJobManager(){
Configuration config = new Configuration.Builder(app)
.minConsumerCount(1).maxConsumerCount(3).loadFactor(3).customLogger(new CustomLogger() {
private static final String TAG = "JOBS";

@Override
public boolean isDebugEnabled() {
return true;
}

@Override
public void d(String text, Object... args) {
Log.d(TAG, String.format(text, args));
}

@Override
public void e(Throwable t, String text, Object... args) {
Log.e(TAG, String.format(text, args));
}

@Override
public void e(String text, Object... args) {
Log.e(TAG, String.format(text, args));
}
})
.consumerKeepAlive(120).build();

return new JobManager(app, config);
}

@Provides @Singleton
public RestAdapter providesRestAdapter()
{
restInstances = new HashMap<String, Object>();
return new RestAdapter.Builder()
.setEndpoint("http://192.168.0.23:9000/api")
.setLogLevel(RestAdapter.LogLevel.FULL).build();
}

那么,如何将这个“缓存”哈希注入(inject)到我的应用程序中?

谢谢

最佳答案

您可以创建一个类,其唯一目的是提供休息接口(interface)类。这是接口(interface)/实现

public interface RestApiProvider {
public <T> T getRestClient(Class<T> clazz);
}

public class RestApiProviderImpl implements RestApiProvider {
private Map<String, Object> restInstances = new HashMap<String, Object>();
private RestAdapter restAdapter;

@Inject
RestApiProvider(RestAdapter restAdapter) {
this.restAdapter = restAdapter;
}

public <T> T getRestClient(Class<T> clazz) {
T client = null;

if ((client = (T) restInstances.get(clazz.getCanonicalName())) != null) {
return client;
}

client = restAdapter.create(clazz);
restInstances.put(clazz.getCanonicalName(), client);
return client;
}

}

在你的模块中你会有

@Provides @Singleton
public RestAdapter providesRestAdapter()
{
return new RestAdapter.Builder()
.setEndpoint("http://192.168.0.23:9000/api")
.setLogLevel(RestAdapter.LogLevel.FULL).build();
}

@Provides @Singleton
public RestApiProvider providesRestApiProvider(RestApiProviderImpl impl) {
return impl;
}

其工作方式是模块中的 RestAdapter Provider 将用作 RestApiProviderImpl 实例中的依赖项。现在,在任何需要获取 RestApi 类实例的地方,您只需注入(inject) RestApiProvider 即可。

@Inject
RestApiProvider restApiProvider;

// Somewhere in your code
RestApiClassOfSomeSort instance = restApiProvider.getRestClient(RestApiClassOfSomeSort.class);
instance.// do what you need!

关于java - 用 Dagger 改造类的 HashMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25025935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com