编辑
Dagger2
本文访问次数:0

首先配置build.gradle文件

// Add Dagger dependencies
dependencies {
    compile 'com.google.dagger:dagger-android:2.x'
    compile 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.x'
}

最新版本为:

使用的dagger版本是最新的2.10
由于Activity是由系统创建,所以使用dagger注入依赖会稍微复杂一点,首先创建一个可注入对象

package me.zongren.dagger2demo;

import javax.inject.Inject;

import dagger.Provides;

/**
 * Created by zongren on 2017/4/19.
 */

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

    public String generate(){
        return String.valueOf(Math.ceil(Math.random() * 99999));
    }
}

创建MainActivitySubcomponent

package me.zongren.dagger2demo;

import dagger.Subcomponent;
import dagger.android.AndroidInjector;

/**
 * Created by zongren on 2017/4/19.
 */

@Subcomponent()
public interface MainActivitySubcomponent extends AndroidInjector<MainActivity>{
    @Subcomponent.Builder
    public abstract class Builder extends AndroidInjector.Builder<MainActivity>{};
}

创建MainActivityModule

package me.zongren.dagger2demo;

import android.app.Activity;

import dagger.Binds;
import dagger.Module;
import dagger.android.ActivityKey;
import dagger.android.AndroidInjector;
import dagger.multibindings.IntoMap;

/**
 * Created by zongren on 2017/4/19.
 */
@Module(subcomponents = MainActivitySubcomponent.class)
 abstract class MainActivityModule {
    @Binds
    @IntoMap
    @ActivityKey(MainActivity.class)
    abstract AndroidInjector.Factory<? extends Activity> bindMainActivityInjectorFactory(MainActivitySubcomponent.Builder builder);

}

创建MainApplicationModule

package me.zongren.dagger2demo;

import android.app.Application;

import dagger.Component;
import dagger.Provides;
import dagger.android.AndroidInjection;
import dagger.android.AndroidInjectionModule;

/**
 * Created by zongren on 2017/4/19.
 */
@Component(modules = {MainActivityModule.class, AndroidInjectionModule.class})
public interface MainApplicationComponent {
    void inject(MainApplication mainApplication);
}

创建MainApplication

package me.zongren.dagger2demo;

import android.app.Activity;
import android.app.Application;
import android.support.v7.app.AppCompatActivity;

import javax.inject.Inject;

import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasDispatchingActivityInjector;
import dagger.android.support.DaggerAppCompatActivity;

/**
 * Created by zongren on 2017/4/19.
 */

public class MainApplication extends Application implements HasDispatchingActivityInjector{
    @Inject
    DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;

    @Override
    public void onCreate() {
        super.onCreate();
        DaggerMainApplicationComponent.create().inject(this);
    }

    @Override
    public DispatchingAndroidInjector<Activity> activityInjector() {
        return dispatchingAndroidInjector;
    }
}

创建MainActivity

package me.zongren.dagger2demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import javax.inject.Inject;

import dagger.android.AndroidInjection;

public class MainActivity extends AppCompatActivity {
    @Inject Generator generator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button)findViewById(R.id.button);
        final TextView textView = (TextView)findViewById(R.id.textView);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                textView.setText(generator.generate());
            }
        });
    }
}

A simple demo of dagger2:Dagger2Demo

需要输入验证码才能留言

没有任何评论