依赖注入的三种形式
- 以参数的形式传入构造器。
- 从其他地方抓取。比如Android中的Context.getSystemService()
- 依赖注入。
使用hit的步骤
- 在项目根级的build.gradle中加入依赖
buildscript {
dependencies {
classpath 'com.google.dagger:hilt-android-gradle-plugin:2.28-alpha'
}
}
复制代码
在app/build.gradle文件中添加依赖
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
dependencies {
implementation "com.google.dagger:hilt-android:2.28-alpha"
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
}
//开启java8支持。
android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
复制代码
-
所有使用 Hilt 的应用都必须包含一个带有
@HiltAndroidApp
注释的Application
类。@HiltAndroidApp class ExampleApplication : Application() { ... } 复制代码
-
将依赖注入Android中的类。
使用
@AndroidEntryPoint
注释的其他 Android 类提供依赖项@AndroidEntryPoint class ExampleActivity : AppCompatActivity() { ... } 复制代码
android 中支持一下这几种类型
Application
(通过使用@HiltAndroidApp
)Activity
Fragment
View
Service
BroadcastReceiver
值得注意的是
- Hilt 仅支持扩展
ComponentActivity
的 Activity,如AppCompatActivity
。 - Hilt 仅支持扩展
androidx.Fragment
的 Fragment。
@AndroidEntryPoint class ExampleActivity : AppCompatActivity() { @Inject lateinit var analytics: AnalyticsAdapter ... } 复制代码
-
构造器注入
class AnalyticsAdapter @Inject constructor( private val service: AnalyticsService ) { ... } 复制代码
-
Hit模块
Hilt 模块是一个带有
@Module
注释的类使用@Binds 注入接口实例
interface AnalyticsService { fun analyticsMethods() } class AnalyticsServiceImpl @Inject constructor( ) : AnalyticsService { ... } @Module @InstallIn(ActivityComponent::class) abstract class AnalyticsModule { @Binds abstract fun bindAnalyticsService( analyticsServiceImpl: AnalyticsServiceImpl ): AnalyticsService } 复制代码
使用@Provides注入实例
@Module @InstallIn(ActivityComponent::class) object AnalyticsModule { @Provides fun provideAnalyticsService( // Potential dependencies of this type ): AnalyticsService { return Retrofit.Builder() .baseUrl("https://example.com") .build() .create(AnalyticsService::class.java) } } 复制代码
同一类型提供多个绑定
定义注解
@Qualifier @Retention(AnnotationRetention.BINARY) annotation class AuthInterceptorOkHttpClient @Qualifier @Retention(AnnotationRetention.BINARY) annotation class OtherInterceptorOkHttpClient 复制代码
定义模块
@Module @InstallIn(ApplicationComponent::class) object NetworkModule { @AuthInterceptorOkHttpClient @Provides fun provideAuthInterceptorOkHttpClient( authInterceptor: AuthInterceptor ): OkHttpClient { return OkHttpClient.Builder() .addInterceptor(authInterceptor) .build() } @OtherInterceptorOkHttpClient @Provides fun provideOtherInterceptorOkHttpClient( otherInterceptor: OtherInterceptor ): OkHttpClient { return OkHttpClient.Builder() .addInterceptor(otherInterceptor) .build() } } // As a dependency of a constructor-injected class. class ExampleServiceImpl @Inject constructor( @AuthInterceptorOkHttpClient private val okHttpClient: OkHttpClient ) : ... // At field injection. @AndroidEntryPoint class ExampleActivity: AppCompatActivity() { @AuthInterceptorOkHttpClient @Inject lateinit var okHttpClient: OkHttpClient } 复制代码
android中预定义的限定符
Activity 的 Context 类@ApplicationContext
和@ActivityContext
限定符。hit有如下组件,并且会管理生命周期。
ApplicationComponent
Application
ActivityRetainedComponent
ViewModel
ActivityComponent
Activity
FragmentComponent
Fragment
ViewComponent
View
ViewWithFragmentComponent
带有 @WithFragmentBindings
注释的View
ServiceComponent
Service
-
组件作用域
默认情况下,Hilt 中的所有绑定都未限定作用域。这意味着,每当应用请求绑定时,Hilt 都会创建所需类型的一个新实例。
-
组件层次结构
-
组件默认绑定
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END