匕首:这方面需要在其构造适当的方式来定义类注射 [英] Dagger: proper way to define injectable class that takes context in its constructor

查看:162
本文介绍了匕首:这方面需要在其构造适当的方式来定义类注射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用匕首(用匕首广场V1)来创建一个单独的类的构造函数需要上下文作为参数。那么我想这个注入单例类到我MainActivity。什么是适当的措施来定义这个?

我试着这样做:

SingletonClass:

  @Module(
    注入= {} MainActivity.class

@辛格尔顿
公共类SingletonClass {
...@注入
SingletonClass(上下文CTX){}
}

而我收到的错误:

 上android.content.Context没有注射成员

据我了解,SingletonClass应该接收它的上下文从为了注射地方,但因为我再也没有传统意义上的呼叫它,而是我在注入它像这样在MainActivity类级别:

  @Inject SingletonClass singletonClass;

我怎么通过它的背景?

下面是我的匕首创建其他文件(其中两个我看到了官方的例子使用):

AndroidModule:

  @Module(库= TRUE,注入= MainActivity.class)
公共类AndroidModule {私人最终上下文的背景下;公共AndroidModule(上下文的背景下){
    this.context =背景;
}/ **
 *允许应用程序上下文被注入,但要求它
 *标注了明确
 *从活动背景区分开来。
 * /
@Provides
@辛格尔顿
@ForApplication
上下文provideApplicationContext(){
    返回语境;
}
}

App.class(延长我的应用程序):

 公共类应用扩展应用{
私有静态最后弦乐TAG = App.class.getSimpleName();
私有静态应用实例;
公共MainActivity mainActivity;
私有静态上下文的背景下;
私人ObjectGraph objectGraph;公共无效的onCreate(){
    super.onCreate();    例如=这一点;
    上下文= getApplicationContext();    objectGraph = ObjectGraph.create(getModules()的toArray());
}公共静态应用的getInstance(){
    返回实例;
}公共静态上下文的getContext(){返回语境; }保护列表与LT;对象> getModules(){
    返回Arrays.asList(新AndroidModule(本),新的App());
}公共无效注射(Object对象){
    objectGraph.inject(对象);
}
}

ForApplication类:

 进口java.lang.annotation.Retention;
    进口javax.inject.Qualifier;    引入静态java.lang.annotation.RetentionPolicy.RUNTIME;@Qualifier @Retention(运行时)
公共@interface ForApplication {
}


解决方案

您需要将 @ForApplication 预选赛添加到您的上下文 SingletonClass 构造函数的参数。

匕首正在寻找 A 上下文注入,但只能有一个 @ForApplication上下文,这是一个不匹配。

  @Singleton
公共类SingletonClass {    @注入
    SingletonClass(@ForApplication上下文CTX){
    }}

现在你也可以得到摆脱了库= TRUE 行的 AndroidModule ,其中你可能已经添加,因为匕首的警告你, @ForApplication上下文是未使用的(不要忽略这些警告!)。

此外,这可能只是一个复制粘贴错误,这个 SingletonClass 不应该有一个 @Module 注释。

I want to use dagger (dagger v1 by Square) to create a singleton class whose constructor requires context as an argument. I then want to inject this singleton class into my MainActivity. What are the proper steps to define this?

I tried doing this:

SingletonClass:

@Module(
    injects = {MainActivity.class}
)
@Singleton
public class SingletonClass {
...

@Inject
SingletonClass(Context ctx) {}
}

to which I receive the error:

no injectable members on android.content.Context

I understand that SingletonClass is supposed to receive it's context from somewhere in order to be injectable, but since I'm not "calling" it anymore in the traditional sense, but rather I'm injecting it like so in the MainActivity at the class-level:

@Inject SingletonClass singletonClass;

how am I supposed to pass it the context?

Here are additional files I created for dagger (two of which I saw used in official examples):

AndroidModule:

@Module(library = true, injects = MainActivity.class)
public class AndroidModule {

private final Context context;

public AndroidModule(Context context) {
    this.context = context;
}

/**
 * Allow the application context to be injected but require that it be
 * annotated with to explicitly
 * differentiate it from an activity context.
 */
@Provides
@Singleton
@ForApplication
Context provideApplicationContext() {
    return context;
}


}

App.class (to extend my application):

public class App extends Application {
private static final String TAG = App.class.getSimpleName();
private static App instance;
public MainActivity mainActivity;
private static Context context;
private ObjectGraph objectGraph;

public void onCreate() {
    super.onCreate();

    instance = this;
    context = getApplicationContext();

    objectGraph = ObjectGraph.create(getModules().toArray());
}

public static App getInstance() {
    return instance;
}

public static Context getContext() { return context; }

protected List<Object> getModules() {
    return Arrays.asList(new AndroidModule(this), new App());
}

public void inject(Object object) {
    objectGraph.inject(object);
}
}

ForApplication class:

import java.lang.annotation.Retention;
    import javax.inject.Qualifier;

    import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Qualifier @Retention(RUNTIME)
public @interface ForApplication {
}

解决方案

You need to add the @ForApplication qualifier to your Context parameter in the SingletonClass constructor.

Dagger is now looking for a Context to inject, but only has a @ForApplication Context, which is a mismatch.

@Singleton
public class SingletonClass {

    @Inject
    SingletonClass(@ForApplication Context ctx) {
    }

}

Now you can also get rid of the library = true line in your AndroidModule, which you've probably added because Dagger warned you that @ForApplication Context was unused (Don't ignore these warnings!).

Also, and that might just be a copy-paste error, this SingletonClass should not have an @Module annotation.

这篇关于匕首:这方面需要在其构造适当的方式来定义类注射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆