我有一个应用程序可以获取预先搜索的类型。我使用dagger进行依赖注入。但是dagger会为活动类注入值,而不会为其他类注入
模块
@Module
public class NetModule {
String baseURL;
public NetModule(String baseURL){
this.baseURL=baseURL;
}
@Provides
@Singleton
SharedPreferences providesSharedPreferences(Application application) {
return PreferenceManager.getDefaultSharedPreferences(application);
}
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}
@Provides
@Singleton
Gson provideGson(){
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(Cache cache) {
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.cache(cache);
return client.build();
}
@Provides
@Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(baseURL)
.client(okHttpClient)
.build();
return retrofit;
}
@Provides
@Singleton
GetTypeAhead provideRGetTypeAhead( Retrofit retrofit) {
return retrofit.create(GetTypeAhead.class);
}
}
我的应用模块
@Module
public class AppModule {
Application mApplication;
public AppModule(Application application) {
mApplication = application;
}
@Provides
@Singleton
Application providesApplication() {
return mApplication;
}
}
my component
@Singleton
@Component(modules={AppModule.class, NetModule.class})
public interface NetComponent {
void inject(MainActivity dataSource);
}
用于匕首初始化的MyTopLevel应用程序组件
public class MyApp extends Application {
private NetComponent netComponent;
@Override
public void onCreate() {
super.onCreate();
// Dagger%COMPONENT_NAME%
netComponent = DaggerNetComponent.builder()
// list of modules that are part of this component need to be created here too
.appModule(new AppModule(this)) // This also corresponds to the name of your module: %component_name%Module
.netModule(new NetModule("https://typeahead.random.com/"))
.build();
// If a Dagger 2 component does not have any constructor arguments for any of its modules,
// then we can use .create() as a shortcut instead:
// mNetComponent = com.codepath.dagger.components.DaggerNetComponent.create();
}
public NetComponent getNetComponent() {
return netComponent;
}
}
我的活动//这里di工作正常
public class MainActivity extends AppCompatActivity {
@Inject
Retrofit retrofit;
CompositeDisposable compositeDisposable = new CompositeDisposable();
private ArrayList<String> arr = new ArrayList<>();
SearchViewModel searchViewModel;
AutoCompleteTextView textView;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchViewModel = ViewModelProviders.of(this).get(SearchViewModel.class);
((MyApp)getApplication()).getNetComponent().inject(this);
Observable<String> inputObservable = setUpTextObservable();
textView= (AutoCompleteTextView) findViewById(R.id.productSearchText);
adapter = new ArrayAdapter<>(this,
android.R.layout.simple_dropdown_item_1line);
Log.v("++++++++++++++++++++++",retrofit.toString());
compositeDisposable.add(searchTextObserver(inputObservable));
}
.....}
我的另一个班级//这里注射不起作用
public class RemoteDataSource {
@Inject
GetTypeAhead getTypeAhead;
Single<TypeAhead> getTypeAhead(String input){
return getTypeAhead.getTypeAhead(input).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
}
我在这里做错了什么。如果我缺乏概念,请引导我进行一些链接
答案 0 :(得分:0)
如果使用属性注入,请执行下一步。在NetComponent
中添加void inject(RemoteDataSource dataSource);
@Singleton
@Component(modules={AppModule.class, NetModule.class})
public interface NetComponent {
void inject(MainActivity dataSource);
void inject(RemoteDataSource dataSource);
}
在RemoteDataSource
类中定义构造函数并在其中进行注入
class RemoteDataSource {
public RemoteDataSource() {
NetComponent netComponent = // TODO get your component
netComponent.inject(this)
}
}
您可以在Dagger Documentation中找到其他信息。
请注意,这并不是以自己的方式使用属性注入的最佳方法。我建议您使用在discussion
中有更多介绍的构造函数注入答案 1 :(得分:0)
要将您的RemoteDataSource
类添加到图形中,您需要在其构造函数中添加@Inject批注并使用scope批注该类:
@Singleton
public class RemoteDataSource {
GetTypeAhead getTypeAhead;
@Inject
RemoteDataSource(GetTypeAhead getTypeAhead){
this.getTypeAhead = getTypeAhead;
}
//...
}
}