Android [SPI,AutoSerivce,ServiceLoader]

编程入门 行业动态 更新时间:2024-10-27 08:35:27

Android [<a href=https://www.elefans.com/category/jswz/34/1768562.html style=SPI,AutoSerivce,ServiceLoader]"/>

Android [SPI,AutoSerivce,ServiceLoader]

记录一下在Android中使用SPI的过程。
1.项目gralde文件。 
plugins {id 'kotlin-kapt'
}
dependencies {implementation 'com.google.auto.service:auto-service:1.0-rc7'                       kapt "com.google.auto.service:auto-service:1.0-rc7"
}   这个AutoService项目的依赖文件需要在项目目录下添加
3. 完整gradle文件如下:
plugins {id 'com.android.application'id 'org.jetbrains.kotlin.android'//kapt作用我们要是用@AutoService注解,需要使用到这个。 id 'kotlin-kapt'
}android {namespace 'com.testpose'compileSdk 33defaultConfig {applicationId "com.testpose"minSdk 21targetSdk 33versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"vectorDrawables {useSupportLibrary true}}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8}kotlinOptions {jvmTarget = '1.8'}buildFeatures {compose true}composeOptions {kotlinCompilerExtensionVersion '1.2.0'}packagingOptions {resources {excludes += '/META-INF/{AL2.0,LGPL2.1}'}}
}dependencies {implementation 'androidx.core:core-ktx:1.7.0'implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'//这个是compose依赖类, implementation 'androidx.activity:activity-compose:1.3.1'implementation "androidxpose.ui:ui:$compose_ui_version"implementation "androidxpose.ui:ui-tooling-preview:$compose_ui_version"implementation 'androidxpose.material:material:1.2.0'androidTestImplementation "androidxpose.ui:ui-test-junit4:$compose_ui_version"debugImplementation "androidxpose.ui:ui-tooling:$compose_ui_version"debugImplementation "androidxpose.ui:ui-test-manifest:$compose_ui_version"//kapt作用我们要是用@AutoService注解,需要使用到这个。 implementation 'com.google.auto.service:auto-service:1.0-rc7'kapt "com.google.auto.service:auto-service:1.0-rc7"testImplementation 'junit:junit:4.13.2'androidTestImplementation 'androidx.test.ext:junit:1.1.5'androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'}
4. SPI代码:

kotlin 代码Service接口代码:

//服务接口类。
interface IComposableService<T> {}//
object ComposableServiceManager {private val composableMap = HashMap<String, IComposableService<*>>()init {ServiceLoader.load(IComposableService::class.java).forEach {composableMap[it.type] = it}}
}/****/
@AutoService(IComposableService::class)
class TextComposable : IComposableService<Model1> {}

5.AutoServicce注解

在 Kotlin 中,@AutoService 注解与 Google 的 AutoService 库一起使用,用于生成用于 Java Service Loader 的元数据文件。这在某些情况下可用于插件化系统,以便在运行时动态加载和发现实现特定接口的类。以下是 @AutoService 在 Kotlin 中的作用:

生成服务提供者配置文件:@AutoService 注解用于生成服务提供者接口的配置文件,该文件通常位于 META-INF/services/ 目录下。这个配置文件通常包含了实现某个接口的类的全限定类名。

Service Loader 支持:Java 提供了 ServiceLoader 类,允许在运行时动态加载实现服务接口的类。@AutoService 注解生成的配置文件是为了与 ServiceLoader 一起使用,使您能够以插件化的方式加载和发现服务实现类。

简化配置:通过使用 @AutoService,您可以避免手动编辑服务提供者配置文件。这有助于减少潜在的配置错误,提高代码的可维护性。

以下是 @AutoService 在 Kotlin 中的基本用法示例:

上面的@AutoServicce注解的作用就是用来生成IComposeableService接口文件的。这个文件记录内容如下: 

com.testpose.base.TitleComposable
6. ServiceLoader的作用。

ServiceLoader 是 Java 中的一个工具类,它的主要作用是在运行时动态加载实现特定接口的类。它通常与 Java Service Provider 接口(SPI)结合使用,允许开发者以插件化的方式扩展应用程序,而无需硬编码或重新编译应用程序。

以下是 ServiceLoader 的主要作用:

  1. 动态加载实现类ServiceLoader 允许您在运行时动态加载实现特定接口的类。这些实现类通常被认为是插件,它们扩展了应用程序的功能,而不需要对应用程序本身进行修改。

  2. 解耦应用程序组件:通过使用 ServiceLoader,应用程序的核心组件可以定义接口,而实际的功能或实现可以通过插件方式提供。这有助于减少组件之间的耦合,使应用程序更加模块化和可维护。

  3. 扩展应用程序功能ServiceLoader 允许开发者轻松地为应用程序添加新的功能或服务,而不必修改或重新编译现有代码。这在创建可扩展的应用程序和框架中非常有用。

  4. 服务提供者配置文件ServiceLoader 需要一个服务提供者配置文件,通常位于 META-INF/services/ 目录下,其中列出了实现特定接口的类的全限定类名。这个配置文件通常是通过 @AutoService 注解生成的。

  5. 多实现支持ServiceLoader 支持多个实现类,您可以同时加载多个实现特定接口的类,并以迭代的方式访问它们。

 

更多推荐

Android [SPI,AutoSerivce,ServiceLoader]

本文发布于:2023-12-03 13:48:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1656042.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SPI   Android   ServiceLoader   AutoSerivce

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!