自己通過apt寫的一個(gè)自動(dòng)生成Component組件的注解工具,主要具有以下功能:
生成Component組件,可指定一個(gè)或多個(gè)Module,可指定scope,并可以添加子組件(SubComponent)
生成子組件SubComponent,并可指定Module,scope,父組件和子組件
自動(dòng)在組件中提供出子組件,并提供inject方法
還能根據(jù)生成的組件,生成DaggerHelper幫助類,更方便的注入
注解種類:
-
GenRootComponent(int tag,int childTag,modules,scope)
該注解用在哪個(gè)類上,就根據(jù)該類生成代碼
生成的Component被@Component注解
參數(shù)含義:
-tag 是自己的標(biāo)識(shí),tag一致的Component為同一組(主要用于父組件查找子組件,不填默認(rèn)為0)
-childTag 是子組件標(biāo)識(shí),工具會(huì)通過該標(biāo)識(shí)來查找所有tag和此標(biāo)識(shí)相等的SubComponent,并在本Component中提供出來
-modules 該組件對(duì)應(yīng)的Module(必填,一個(gè)或多個(gè))
-scope 該組件對(duì)應(yīng)的scope(選填)
@GenRootComponent(scope = Singleton.class, modules = {AppModule.class}, tag = 10, childTag = 11)
public class App extends Application {
}
生成的代碼
@Component(
modules = AppModule.class
)
@Singleton
public abstract interface AppComponent extends MembersInjector<App> {
BaseActivityComponent.Builder providerBaseActivityComponent();
@Component.Builder
abstract interface Builder {
AppComponent build();
Builder setModule(AppModule module);
}
}
-
GenSubComponent(int tag,int childTag,modules,scope)
該注解用在哪個(gè)類上,就根據(jù)該類生成代碼
生成的Component被@Subcomponent注解
生成的Subcomponent依然可以有子組件注解
參數(shù)含義:
-tag 是自己的標(biāo)識(shí),tag一致的Component為同一組(主要用于父組件查找子組件,不填默認(rèn)為0)
-childTag 是子組件標(biāo)識(shí),工具會(huì)通過該標(biāo)識(shí)來查找所有tag和此標(biāo)識(shí)相等的SubComponent,并在本Component中提供出來
-modules 該組件對(duì)應(yīng)的Module(必填,一個(gè)或多個(gè))
-scope 該組件對(duì)應(yīng)的scope(選填)
@GenSubComponent(modules = ActivityModule.class, tag = 12)
public class MainActivity extends BaseActivity {
}
生成的代碼
@Subcomponent(
modules = ActivityModule.class
)
public abstract interface MainActivityComponent extends
//提供子組件
MembersInjector<MainActivity> {
@Subcomponent.Builder
abstract interface Builder {
MainActivityComponent build();
Builder setModule(ActivityModule module);
}
}
-
@GenInheritedSubComponent(int tag,int childTag,modules,scope)
有時(shí)可能需要在基類中注入需要的對(duì)象,這樣的話,每一個(gè)該基類的實(shí)現(xiàn)類都需要生成Component,并注入該實(shí)現(xiàn)類,才能保證基類中的對(duì)象不為空,有時(shí)并不是每個(gè)Activity或Fragment都需要自己的Component,這時(shí)就可以用該注解,該注解適合使用在基類中,生成基類的Component,如果某一個(gè)實(shí)現(xiàn)類不需要Component,就可以使用基類的Component,并且通過該注解生成的Component,會(huì)提供所有沒有自己的Component的實(shí)現(xiàn)類的inject()方法來注入自己(該步驟可在基類中完成)
其他功能與GenSubComponent一致
參數(shù)含義:
-tag 是自己的標(biāo)識(shí),tag一致的Component為同一組(主要用于父組件查找子組件,不填默認(rèn)為0)
-childTag 是子組件標(biāo)識(shí),工具會(huì)通過該標(biāo)識(shí)來查找所有tag和此標(biāo)識(shí)相等的SubComponent,并在本Component中提供出來
-modules 該組件對(duì)應(yīng)的Module(必填,一個(gè)或多個(gè))
-scope 該組件對(duì)應(yīng)的scope(選填)
@GenInheritedSubComponent(tag = 11, childTag = 12, modules = BaseModule.class, scope = BaseScope.class,shouldInject = false)
public class BaseActivity extends AppCompatActivity
生成的代碼
@Subcomponent(
modules = BaseModule.class
)
@BaseScope
public abstract interface BaseActivityComponent {
//該Activity沒有自己的Component,則提供注入方法
void injectMembers(AppActivity target);
SplashActivityComponent.Builder providerSplashActivityComponent();
MainActivityComponent.Builder providerMainActivityComponent();
@Subcomponent.Builder
abstract interface Builder {
BaseActivityComponent build();
Builder setModule(BaseModule module);
}
}
@GenDaggerHelper
功能通過該注解,可以生成DaggerHelper輔助類(單例)提供注入方法
該注解可使用在任何一個(gè)類上(主要為了提供一個(gè)包來存放該輔助類)
生成的注入方法時(shí)分層級(jí)的,就是說,必須先注入該組件的父組件,才能注入該組件,不然會(huì)空指針(因?yàn)楦附M件還沒獲取到)
@GenInheritedSubComponent注解的基類的子類(沒有自己的Component的子類)統(tǒng)一提供一個(gè)注入方法,會(huì)根據(jù)傳入的類型,自動(dòng)找到inject()方法來注入(也就是說可以在基類中注入)
生成的注入方法對(duì)應(yīng)的Module構(gòu)造函數(shù)若有參數(shù),則注入時(shí)也需要傳入對(duì)應(yīng)的參數(shù)
具體來看生產(chǎn)代碼
public class DaggerHelper {
public AppComponent appcomponent;
private BaseActivityComponent baseactivitycomponent;
private DaggerHelper() {
}
public static DaggerHelper getInstance() {
return Holder.INSTANCE;
}
public AppComponent getAppComponent(App target, Application param0) {
appcomponent = DaggerAppComponent.builder().setModule(new AppModule(param0)).build();
appcomponent.injectMembers(target);
return appcomponent;
}
//因BaseModule()有參數(shù),所以這里也需要傳參(param0)
public BaseActivityComponent getBaseActivityComponent(Object target, Activity param0) {
baseactivitycomponent = appcomponent.providerBaseActivityComponent().setModule(new BaseModule(param0)).build();
if(target instanceof AppActivity) {
AppActivity appactivity = (AppActivity)target;
baseactivitycomponent.injectMembers(appactivity);
}
return baseactivitycomponent;
}
public MainActivityComponent getMainActivityComponent(MainActivity target, Activity param0) {
MainActivityComponent mainactivitycomponent = baseactivitycomponent.providerMainActivityComponent().setModule(new ActivityModule(param0)).build();
mainactivitycomponent.injectMembers(target);
return mainactivitycomponent;
}
public SplashActivityComponent getSplashActivityComponent(SplashActivity target) {
SplashActivityComponent splashactivitycomponent = baseactivitycomponent.providerSplashActivityComponent().setModule(new SpashModule()).build();
splashactivitycomponent.injectMembers(target);
return splashactivitycomponent;
}
private static class Holder {
private static final DaggerHelper INSTANCE = new DaggerHelper();
}
}
庫的介紹就到這里了,希望對(duì)大家有所幫助!!!
下面是項(xiàng)目地址:
注:項(xiàng)目中還有兩個(gè)其他的注解工具,以后有時(shí)間再介紹
gradle:
compile "com.suhang.lazy:lazyannotation_annotation:1.1.8"
compile "com.suhang.lazy:lazyannotation:1.1.8"
annotationProcessor "com.suhang.lazy:lazyannotation_compiler:1.1.8"