《Android进阶之光》--Dagger2

Posted 嘉禾世兴

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了《Android进阶之光》--Dagger2相关的知识,希望对你有一定的参考价值。

No1:

Project的build.gradle文件添加

buildscript{
    dependencies{
        ...classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8‘
    }
}

Module:app的build.gradle添加

apply plugin: ‘com.neenbedankt.android-apt‘
...
dependencies{
    ...
    apt ‘com.google.dagger:dagger-compiler:2.7‘
    compile ‘com.google.dagger:dagger:2.7‘
}

No2:

@Inject、@Component

public class Watch{
    @Inject
    public Watch(){
    }
    public void work(){
    }
}
@Component
public interface MainActivityComponent{
    void inject(MainActivity activity);
}

调用

public class MainActivity extends AppCompatActivity{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DaggerMainActivityComponent.create().inject(this);//2
    watch.work();
}

No3:

@Module、@Provides

@Inject
Gson gson;
@Module
public class GsonModule{
    @Provides
    public Gson provideGson(){
        return new Gson();
    }
}
@Component(modules = GsonModule.class)
public interface MainActivityComponent{
    void inject(MainActivity activity);
}

调用

public class MainActivity extends AppCompatActivity{
    @Inject
    Gson gson;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DaggerMainActivityComponent.create().inject(this);
        String jsonData = "{‘name‘:‘zhangwuji‘,‘age‘:20}";
        Man man = gson.fromJson(jsonData,Man.class);
    }
}

No4:

@Named、@Qualifier

@Module
public class EngineModule{
    @Provides
    @Named("Gasoline")
    public Engine provideGasoline(){
        return new GasolineEngine();
    }
    @Provides
    @Named("Diesel")
    public Engine provideDiesel(){
        return new DieselEngine();
    }
}

调用

public class Car{
    private Engine engine;
    @Inject
    public Car(@Named("Diesel") Engine engine){
        this.engine = engine;
    }
    public String run(){
        return engine.work();
    }
}

-------------------------------------

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

@Qualifier
@Retention(RUNTIME)
public @interface Diesel{
}
@Module
public class EngineModule{
    @Provides
    @Gasonline
    public Engine provideGasoline(){
        return new GasolineEngine();
    }
    @Provides
    @Diesel
    public Engine provideDiesel(){
        return new DieselEngine();
    }
}
public class Car{
    private Engine engine;
    @Inject
    public Car(@Gasoline Engine engine){
        this.engine = engine;
    }
    public String run(){
        return engine.work();
    }
}

No5:

 @Singleton、@Scope

@Inject
Gson gson;
@Inject
Gson gson1;
@Module
public class GsonModule{
    @Singleton
    @Provides
    public Gson provideGson(){
        return new Gson();
    }
}
@Singleton
@Component(modules = GsonModule.class)
public interface MainActivityComponent{
    void inject(MainActiviy activity);
}

调用

public class MainActivity extends AppCompatActivity{
    private static final String TAG="Dagger2";
    @Inject
    Gson gson;
    @Inject
    Gson gson1;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        DaggerMainActivityComponent.create().inject(this);
    }
}

-----------------------------

@Scope
@Documented
@Retention(RUNTIME)
public @interface Singleton{}
@Scope
@Rentention(RUNTIME)
public @interface ApplicationScope{}
@Module
public class GsonModule{
    @ApplicationScope
    @Provides
    public Gson provideGson(){
        return new Gson();
    }
}
@ApplicationScope
@Component(modules=GsonModule.class)
public interface ActivityComponent{
    void inject(MainActivity activity);
    void inject(SecondActivity activity);
}
public class App extends Application{
    ActivityComponent activityComponent;
    @Override
    public void onCreate(){
        super.onCreate();
        activityComponent = DaggerActivityComponent.builder().build();
    }
    public static App get(Context context){
        return (App)context.getApplicationContext();
    }
    ActivityComponent getActivityComponent(){
        return activityComponent;
    }
}

调用

public class MainActivity extends AppCompatActivity{
    private static final String TAG="Dagger2";
    private Button bt_jump;
    @Inject
    Gson gson;
    @Inject
    Gson gson1;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        App.get(MainActivity.this).getActivityComponent().inject(this);
        onClick();
    }
    private void onClick(){
        bt_jump = (Button)findViewById(R.id.bt_jump);
        bt_jump.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                Intent intent = new Intent(MainActivity.this,SecondActivity.class);
                startActivity(intent);
            }
        });
    }
}

No6:

@Component

public class Swordsman{
    @Inject
    public Swordsman{
    }
    public String fighting(){
        return "欲为大树,莫与草争";
    }
}
@Module
public class SwordsmanModule{
    @Provides
    public Swordsman provideSwordsman(){
        return new Swordsman();
    }
}

@Component(modules = SwordsmanModule.class)
public interface SwordsmanComponent{
    Swordsman getSwordsman();
}
@ApplicationScope
@Component(modules = GsonModule.class,dependencies = SwordsmanComponent.class)
public interface ActivityComponent{
    void inject(MainActivity activity);
    void inject(SecondActivity activity);
}
public class App extends Application{
    ActivityComponent activityComponent;
    @Override
    public void onCreate(){
        super.onCreate();
        activityComponent = DaggerActivityComponent.builder();
        swordsmanComponent(DaggerSwordsmanComponent.builder().build()).build();
    }
    public static App get(Context context){
        return (App)context.getApplicationContext();
    }
    ActivityComponent getActivityComponent(){
        return activityComponent;
    }
}

调用

public class SecondActivity extends AppCompatActivity{
    @Inject
    Swordsman swordsman;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        App.get(SecondActivity.this).getActivityComponent().inject(this);
        String sd = swordsman.fighting(;
    }
}

No7:

懒加载:在@Inject的时候不初始化,而是使用的时候,调用get方法来获取实例

public class SecondActivity extends AppCompatActivity{
    private static final String TAG="Dagger2";
    @Inject
    Lazy<Swordsman> swordsmanLazy;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        App.get(SecondActivity.this).getActivityComponent().inject(this);
        Swordsman swordsman = swordsmanLazy.get();
        swordsman.fighting();
        String sd1 = swordsman.fighting();
    }
}

 

以上是关于《Android进阶之光》--Dagger2的主要内容,如果未能解决你的问题,请参考以下文章

入门实战资料《Android进阶解密》+《Android进阶之光》+《重构改善既有的代码第2版》电子资料学习

进阶三部曲第一部《Android进阶之光》第2版已出版

进阶三部曲第一部《Android进阶之光》第2版已出版

进阶三部曲第一部《Android进阶之光》第2版已出版

进阶三部曲第一部《Android进阶之光》第2版已出版

《Android进阶之光》--Material Design