史上最适合新手的Dagger2教程(一)基本注入

码字辛苦!转载请注明出处!

Dagger2系列教程目录:

史上最适合新手的Dagger2教程(一)基本注入

史上最适合新手的Dagger2教程(二)对象注入

史上最适合新手的Dagger2教程(三)模型与单例

 史上最适合新手的Dagger2教程(四)带参注入

史上最适合新手的Dagger2教程(五)命名、限定与延时加载

0.前言

Dagger2是今年5月份开始,突然火到一发不可收拾的框架,然而,网上对于它的评价,往往是门槛高、从入门到放弃……

——其实这只是不善于表达的传授者的说辞而已,Dagger2并不难,只是你没有找对教程而已。

当你打开这篇教程的时候,恭喜你,你的研究之路终于要苦尽甘来了~

1.Dagger2是什么

Dagger2是一个通过Java注解的手段,省下大量new对象的代码,并达成解耦目的的框架。也就是老生常谈的依赖注入框架。

有些读者就要抬杠了:new一个对象多简单啊,为什么非要使用Dagger2呢?

朋友,那是因为你没有见过大场面,这里节选一段博主从开发生涯中某项目中节选的一段代码:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        pMain = new PMain(this);
        daoSession = getmApplication().getDaoSession();
        user = new User(daoSession);
        customerOrderInfomation = new CustomerOrderInfomation(daoSession);
        aMapSmoothMarkerUtils = new AMapSmoothMarkerUtils();
        fMainRate = new FMainRate();
        fMainLocationSelector = new FMainLocationSelector();
        fDataPicker = new FDataPicker();
        orderCache = new OrderCache();
        locationCache = new LocationCache();
        orderBuilder= new OrderBuilder(locationCache,orderCache);
        ......

在使用Dagger2后,上面的代码变成了这样:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        DaggerMainActivityComponent.create().inject(this);

这样一来,不仅节省了大量代码。在某天,其中一些对象因为新需求的原因,改动了构造方法,使用这些对象的地方甚至不需要修改任何代码!

朋友,现在还觉得Dagger2并没有什么卵用吗?

那么我们开始实际使用吧~

2.配置

和其他SDK一样,Dagger2也可以通过gradle直接配置:

    implementation 'com.google.dagger:dagger:2.16'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.16'

最新版本请在https://github.com/google/dagger上获取。

3.基本使用

我们先来编写一个以常规手段开发的DEMO:

先编写一个卖萌类,它的方法就是卖萌:

public class SellMoe {

    public String sellMoe() {
        return "赶紧卖了个大萌";
    }
}

把它用在TextView上:

public class MainActivity extends AppCompatActivity {

    SellMoe sellMoe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = findViewById(R.id.tv);
        sellMoe = new SellMoe();
        tv.setText(sellMoe.sellMoe());
    }
}

运行效果如下:

现在,我们使用Dagger2为我们处理这个卖萌类:

Step1:注入标记(@Inject)

首先为卖萌类创建一个构造方法,用@Inject注释标记起来:

public class SellMoe {
    @Inject
    public SellMoe() {
    }

    public String sellMoe() {
        return "赶紧卖了个大萌";
    }
}

Dagger2通过这个标记调用它的构造方法(其实就是在帮你 new SellMoe();)。

 同样的,在使用它的地方标记@Inject:

public class MainActivity extends AppCompatActivity {

    @Inject
    SellMoe sellMoe;

    ......

有没有觉得很熟悉?对,它和它的兄弟ButterKnife太像了:

    @BindView(R.id.textView)
    TextView textView;

他们俩都是出自Square公司的安卓大神JakeWharton之手,ButterKnife,Retrofit,OkHttp被称为Square全家桶。

好了,扯远了,我们继续玩Dagger2.

Step2:编写注入器(@Component)

注入器是一个被@Component标记接口,它将注入方法提供给Dagger2:

这里因为是给MainActivity注入,所以取名叫MainActivityComponent。

@Component
public interface MainActivityComponent {
    void inject(MainActivity mainActivity);
}

Dagger2正是通过这个接口,为你实现依赖注入的。

Step3:构建项目,生成注入器

Make一下Project,喝口冰阔落。

Step4:使用注入器

Build完成后,我们就可以删掉原来的new,替换成Dagger2的注入器了:

public class MainActivity extends AppCompatActivity {

    @Inject
    SellMoe sellMoe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = findViewById(R.id.tv);
//        sellMoe = new SellMoe();
        DaggerMainActivityComponent.create().inject(this);
        tv.setText(sellMoe.sellMoe());
    }
}

这个DaggerMainActivityComponent正是Dagger2通过我们编写的MainActivityComponent生成的,名字前面加了个Dagger而已。

DaggerMainActivityComponent.create().inject(this);是不是像极了ButterKnife的ButterKnife.bind(this);?

不同的是,这里的.inject(this)方法是我们自己声明的。

4.家庭作业

邓爷爷曾经说过:实践是检验真理的唯一标准。

这个框架其实并不难,只是比ButterKnife的代码稍微多了那么一点点,因此需要更多的练习来消化。

话不多说,今天的作业是:

1、编写一个杠精类,它的功能就是抬杠,使用Dagger2对他进行实例化并调用:

public class GangJing {

    public void gang(Context context) {
        Toast.makeText(context, "这抠脚大汉天天卖萌", Toast.LENGTH_SHORT).show();
    }
}

2、给博主发个红包吧~

完成作业之后,咱们再进入第二章

猜你喜欢

转载自blog.csdn.net/u014653815/article/details/81201865