匕首2:未注入场

时间:2019-03-08 18:48:42

标签: android dagger-2 dagger

我从未在此类中进行改造的字段,在我运行代码时它仍然为null。

这是我的ServiceClass,我在其中注入改造,进行api调用等。为简单起见,我将其剥离:

public class ServiceClass{

    @Inject
    Retrofit retrofit;

    public ServiceClass(){
    }

}

所有与网络相关的依赖项的模块类:

@Module
public class NetworkModule {

    @Provides
    @ApplicationScope
    Retrofit getRetrofit(OkHttpClient okHttpClient, Gson gson){
        return new Retrofit.Builder()
                .baseUrl(URL.BASE_URL)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
    }

    @Provides
    @ApplicationScope
    OkHttpClient getOkHttpClient(Gson gson, HttpLoggingInterceptor httpLoggingInterceptor){
        OkHttpClient okHttpClient = new OkHttpClient();
        okHttpClient.newBuilder().addInterceptor(httpLoggingInterceptor);
        return okHttpClient;
    }

    @Provides
    @ApplicationScope
    HttpLoggingInterceptor getHttpLoggingInterceptor(){
        return new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC);
    }

    @Provides
    @ApplicationScope
    Gson getGson(){
        return new Gson();
    }

}

我的AppComponent这是我唯一的组件类:

@ApplicationScope
@Component(modules = {NetworkModule.class})
public interface AppComponent {

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(MyApplication myApplication);
        AppComponent build();
    }

    void inject(MyApplication myApplication);

    Retrofit getRetrofit();
}

我的应用程序类:

public class MyApplication extends Application{

    private AppComponent appComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        DaggerAppComponent
                .builder()
                .application(this)
                .build()
                .inject(this);
    }

    public AppComponent getAppComponent(){
        return appComponent;
    }

}

我试图弄乱代码,但似乎无法设法使其正常工作。我在这里想念什么?

2 个答案:

答案 0 :(得分:1)

更新(以前的信息仍然有效):

我注意到您错误地构建了组件:您必须在.networkModule(new NetworkModule())之后添加DaggerAppComponent.builder() 确保您的private AppComponent appComponent也已初始化!


对于字段注入(我相信这就是您要追求的),可以这样编写构造函数:

public ServiceClass(){
    MyApplication.getInstance().getAppComponent().inject(this)
}

自然,您应该以某种方式公开您的appComponent实体-以上是我的猜测(通过应用程序实体公开appComponent实体)。

PS:更好的方法(也更具可读性)是完全避免字段注入和参数化构造函数(但是并非总是可能的,例如,如果您注入活动)。

PSS 。:您的AppComponent还应该具有void inject(ServiceClass value);

答案 1 :(得分:1)

有多种方法将Get-CsAdUser : Management object not found for identity "person1". At line:3 char:1 + Get-CsAdUser -Identity $line.Name|Set-CsUser -SipAddress $goodsip -wh ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (person1:UserIdParameter) [Get-CsAdUser], ManagementException + FullyQualifiedErrorId : Identity,Microsoft.Rtc.Management.AD.Cmdlets.GetAdUserCmdlet 注入retrofit

  1. 您必须为ServiceClass做一个单独的Component,例如:-

    ServiceClass

  1. 您只需将@Component(dependencies = AppComponent.class) interface ServiceClassComponent { void injectServiceClass(ServiceClass serviceClass); } 注入到您的应用程序组件中:-

    ServiceClass

进入您的void injectServiceClass(ServiceClass serviceClass);

AppComponent关键字会将所有从属组件包括在要构建的特定组件中。

然后在dependencies的构造函数中,您需要构建该组件并将其注入

相关问题