注入方法是保留名称吗? Dagger-2如何知道如何实现其机体? [英] Is the inject method a reserved name? how Dagger-2 knows to implement its body?
问题描述
我见过以下代码段:
@Component(modules = {TestActivityModule.class})
public interface TestActivityComponent {
void inject(TestActivity activity);
}
但是注入
方法未在用户代码中实现(而是在 Dagger-2
代码中自动生成)。
But the inject
method is not implemented in user code (but auto-generated in Dagger-2
code).
注入
保留名称?
推荐答案
好的,怎么了, Dagger-2
如何实现此方法呢?我明白了:名字没关系,可以是例如压缩
,只要提供的类型包含 @Inject
字段/方法/构造函数), Dagger-2
将生成方法的正文:
Ok, I got it: the name doesn't matter, it can be e.g. squeeze
, as long as the provided type contains @Inject
fields(s)/methods(s)/constructor(s), Dagger-2
will generate the method's body:
@Component(modules = {TypoModule.class})
public interface TypoComponent {
void squeeze(Thingie t);
}
...,只要存在 @提供
,其返回 @Inject
ed类型:
...and as long as there's a @Provides
that returns the @Inject
ed type:
@Module class TypoModule {
@Provides InjectedType whateverNameYouDecide() {
return new InjectedSubType();
// InjectedSubType extends InjectedType, obviously...
}
}
当然, Thingie
应该具有 @Inject
ed成员,否则什么也不会发生:
Of course, Thingie
should have @Inject
ed member or nothing will happen:
class Thingie {
@Inject InjectedType thingieID;
}
这就是整个故事...
That's the whole story...
这篇关于注入方法是保留名称吗? Dagger-2如何知道如何实现其机体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!