青春须早为,岂能长少年。这篇文章主要讲述《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 = "https://www.songbingjia.com/android/{‘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】
推荐阅读
- [LeetCode] Find Anagram Mappings 寻找异构映射
- 《Android进阶之光》--事件总线
- jQuery中append appendTo prepend prependTo insertBefore insertAfter after before之间的区别
- android studio gradle统一管理版本
- 全民充电节APP又上新福利,你发现了吗()
- Android Studio 经常使用手冊
- 手工增加Mapping
- 使用ionic cordova build android --release --prod命令打包报错解决方法
- android app 闪屏