注入方法是保留名称吗? Dagger-2如何知道如何实现其机体? [英] Is the inject method a reserved name? how Dagger-2 knows to implement its body?

查看:54
本文介绍了注入方法是保留名称吗? Dagger-2如何知道如何实现其机体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过以下代码段:

@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 @Injected type:

@Module class TypoModule {
    @Provides InjectedType whateverNameYouDecide() {
        return new InjectedSubType();
        // InjectedSubType extends InjectedType, obviously...
    }
}

当然, Thingie 应该具有 @Inject ed成员,否则什么也不会发生:

Of course, Thingie should have @Injected member or nothing will happen:

class Thingie {
    @Inject InjectedType thingieID;
}

这就是整个故事...

That's the whole story...

这篇关于注入方法是保留名称吗? Dagger-2如何知道如何实现其机体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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