简体   繁体   English

Dagger 2组件依赖不起作用

[英]Dagger 2 component dependency not working

The dagger modules 匕首模块

@Module
public class NetModule {

    @Provides
    @Singleton
    Gson provideGson() {
        GsonBuilder gsonBuilder = new GsonBuilder();
        return gsonBuilder.create();
    }

    @Provides
    @Singleton
    OkHttpClient provideOkHttpClient() {
        OkHttpClient client = new OkHttpClient();
        return client;
    }

    @Provides
    @Singleton
    Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create(gson))
                .baseUrl(BuildConfig.SERVER_BASE_URL)
                .client(okHttpClient)
                .build();
        return retrofit;
    }
}

@Module
public class AppModule{

    private Application application;

    public AppModule(Application application){
        this.application = application;
    }

    @Provides
    @Singleton
    Application provideApplication() {
        return application;
    }
}

@Module
public class ImageModule {

    @Provides
    @Singleton
    Picasso providePicasso(Application application, OkHttpClient client) {
        Picasso.Builder builder = new Picasso.Builder(application);
        builder.downloader(new OkHttp3Downloader(client));
        return builder.build();
    }

}

These are the components 这些是组件

@Singleton
@Component(modules={NetModule.class})
public interface NetComponent {

    void inject(MyFragment fragment);
}

@Singleton
@Component(modules={AppModule.class, ImageModule.class}, dependencies = {NetModule.class})
public interface ImageComponent {

    void inject(MyFragment fragment);
}

This is how I am registering the components 这就是我注册组件的方式

public class MyApp extends Application{

    @Override
    public void onCreate() {

        netComponent = DaggerNetComponent.builder()
                .netModule(new NetModule())
                .build();

        imageComponent = DaggerImageComponent.builder()
                .appModule(new appModule(this))
                .imageModule(new ImageModule())
                .build();

    }

}

And in the fragment 并在片段中

public class MyFragment extends Fragment {

   @Inject
   Retrofit retrofit;

   @Inject
   Picasso picasso;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,   
     Bundle savedInstanceState) {

   ((MyApp)getActivity().getApplication()).getNetComponent().inject(this);

   ((MyApp)getActivity().getApplication()).getImageComponent().inject(this);
  ....

  }


}

I am getting compilation error as below 我收到编译错误如下

Error:(25, 10) error: com.squareup.picasso.Picasso cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method. 错误:(25,10)错误:如果没有@Inject构造函数或@ Provide-或@ Produces-annotated方法,则无法提供com.squareup.picasso.Picasso。

What is the best way to achieve this in dagger 2? 在匕首2中实现这一目标的最佳方法是什么?

When Component A depends on type B, you're saying that every zero-arg getter on B will be available in A's graph . 当组件A依赖于类型B时,你会说B上的每个零弧度吸气器都将在A的图形中可用 This means that you don't need to call inject separately on NetComponent, but you do need to expose OkHttpClient in NetComponent's definition. 这意味着您不需要在NetComponent上单独调用inject ,但是您需要在NetComponent的定义中公开OkHttpClient。

This means NetComponent would look like this: 这意味着NetComponent看起来像这样:

@Singleton
@Component(modules={NetModule.class})
public interface NetComponent {
    /** Exposes OkHttpClient. Used by components depending on NetComponent. */
    OkHttpClient getOkHttpClient();
}

And your onCreateView could look like this: 你的onCreateView看起来像这样:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,   
    Bundle savedInstanceState) {

  ((MyApp)getActivity().getApplication()).getImageComponent().inject(this);

}

Internally, Dagger generates the DaggerNetComponent and DaggerImageComponent in two separate steps, but has ImageComponent call the new getOkHttpClient method . 在内部,Dagger在两个单独的步骤中生成DaggerNetComponent和DaggerImageComponent,但ImageComponent 调用新的getOkHttpClient method This also means that DaggerImageComponent can accept any implementation of NetComponent, not just the one Dagger generates. 这也意味着DaggerImageComponent可以接受NetComponent的任何实现,而不仅仅是Dagger生成的那个。

Related resources: 相关资源:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM