我正在尝试使用 Dagger2 做一些事情,但仍然难以理解..
我想在 2 个类中使用 2 个服务,SplashActivity 和 HomeActivity。 服务依赖于 NetModule,因为我想重用改造和 okhttpclient 提供。
这是我的网络模块:
@Module
public class NetModule {
@Provides
Retrofit provideRetrofit(@Named("BaseUrl") String baseUrl, OkHttpClient okHttpClient) {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
@Provides
HttpLoggingInterceptor provideHttpLoggingInterceptor() {
HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
if (BuildConfig.DEBUG) {
logging.setLevel(HttpLoggingInterceptor.Level.HEADERS);
} else {
logging.setLevel(HttpLoggingInterceptor.Level.NONE);
}
return logging;
}
@Provides
OkHttpClient provideOkHttpClient(HttpLoggingInterceptor httpLoggingInterceptor, @Named("ConnectTimeoutMs") int connectTimeoutMs, @Named("ReadTimeoutMs") int readTimeoutMs) {
return new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.connectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS)
.readTimeout(readTimeoutMs, TimeUnit.MILLISECONDS)
.build();
}
}
StaticDataModule 和 StatusModule,它是 2 个不同的 API,因此它们使用 NetModule:
@Module(includes = NetModule.class)
public class StaticDataModule {
@Provides
@Singleton
StaticDataService provideStaticDataService(Retrofit retrofit) {
return new RetrofitStaticDataService(retrofit);
}
@Provides
@Named("BaseUrl")
String provideBaseUrl() {
return "http://url_static.com";
}
@Provides
@Named("ConnectTimeoutMs")
int provideConnectTimeoutMs() {
return 5000;
}
@Provides
@Named("ReadTimeoutMs")
int provideReadTimeoutMs() {
return 5000;
}
}
@Module(includes = NetModule.class)
public class StatusModule {
@Provides
@Singleton
StatusService provideStatusService(Retrofit retrofit) {
return new RetrofitStatusService(retrofit);
}
@Provides
@Named("BaseUrl")
String provideBaseUrl() {
return "http://url_status.com";
}
@Provides
@Named("ConnectTimeoutMs")
int provideConnectTimeoutMs() {
return 5000;
}
@Provides
@Named("ReadTimeoutMs")
int provideReadTimeoutMs() {
return 5000;
}
}
我在这个组件中构建它们:
@Singleton
@Component(modules = {AppModule.class, StaticDataModule.class, StatusModule.class})
public interface AppComponent {
void inject(SplashActivity splashActivity);
void inject(HomeActivity homeActivity);
}
我收到此错误:@javax.inject.Named("BaseUrl") java.lang.String 被多次绑定(bind)。 我理解我的错误,dagger 不知道谁在 StaticData baseUrl 和 Status baseUrl 之间提供。
我试图制作 2 个组件,StaticDataComponent 和 StatusComponent,但我不能在同一个 Activity 中注入(inject)这两个组件。
我尝试在 StaticDataModule 和 StatusModule 中扩展 NetModule,并使用构造函数提供参数,但在改造提供时出现多边界错误。
所以我不知道如何在 2 个模块中重用具有不同参数的 NetModule,如果有人有示例,它应该真的对我有帮助。
谢谢!
最佳答案
不要将 NetModule.class 作为模块的依赖项包含在内,只需将它们单独包含在组件中即可。
@Module(includes = NetModule.class)
public class StaticDataModule {
到
@Module
public class StaticDataModule {
然后像这样使用:
@Component(modules = {
NetModule.class, StaticDataModule.class
}) public interface FirstComponent {
void inject(WhateverYouWantActivity activity);
}
@Component(modules = {
NetModule.class, StatusModule.class
}) public interface FirstComponent {
void inject(WhateverYouWantSecondActivity activity);
}
如果你必须注入(inject)相同的 Activity ,你最好重新设计你的架构,但如果你仍然想这样做,你可以去掉注入(inject)方法,并添加如下内容:
@Component(modules = { /* your modules */ }) public interface YourComponent {
Retrofit getRetrofit();
OkHttpClient getOkHttpClient();
}
并相应地使用它:
retrofit = yourComponentBuiltByDagger.getRetrofit();
代替:
@Inject Retrofit retrofit;
关于java - Android Dagger2 错误 : @javax. inject.Named ("BaseUrl") java.lang.String 被多次绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39526838/
我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
对于作为String#tr参数的单引号字符串文字中反斜杠的转义状态,我觉得有些神秘。你能解释一下下面三个例子之间的对比吗?我特别不明白第二个。为了避免复杂化,我在这里使用了'd',在双引号中转义时不会改变含义("\d"="d")。'\\'.tr('\\','x')#=>"x"'\\'.tr('\\d','x')#=>"\\"'\\'.tr('\\\d','x')#=>"x" 最佳答案 在tr中转义tr的第一个参数非常类似于正则表达式中的括号字符分组。您可以在表达式的开头使用^来否定匹配(替换任何不匹配的内容)并使用例如a-f来匹配一