標籤:get pos protect ica provides dac his apply sel
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(){ }}
@Componentpublic 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
@InjectGson gson;
@Modulepublic 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
@Modulepublic 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{}
@Modulepublic 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
@InjectGson gson;@InjectGson gson1;
@Modulepublic 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{}
@Modulepublic 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 "欲為大樹,莫與草爭"; }}
@Modulepublic 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