• 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文件如下:
    1. plugins {
    2. id 'com.android.application'
    3. id 'org.jetbrains.kotlin.android'
    4. //kapt作用我们要是用@AutoService注解,需要使用到这个。
    5. id 'kotlin-kapt'
    6. }
    7. android {
    8. namespace 'com.test.compose'
    9. compileSdk 33
    10. defaultConfig {
    11. applicationId "com.test.compose"
    12. minSdk 21
    13. targetSdk 33
    14. versionCode 1
    15. versionName "1.0"
    16. testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    17. vectorDrawables {
    18. useSupportLibrary true
    19. }
    20. }
    21. buildTypes {
    22. release {
    23. minifyEnabled false
    24. proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    25. }
    26. }
    27. compileOptions {
    28. sourceCompatibility JavaVersion.VERSION_1_8
    29. targetCompatibility JavaVersion.VERSION_1_8
    30. }
    31. kotlinOptions {
    32. jvmTarget = '1.8'
    33. }
    34. buildFeatures {
    35. compose true
    36. }
    37. composeOptions {
    38. kotlinCompilerExtensionVersion '1.2.0'
    39. }
    40. packagingOptions {
    41. resources {
    42. excludes += '/META-INF/{AL2.0,LGPL2.1}'
    43. }
    44. }
    45. }
    46. dependencies {
    47. implementation 'androidx.core:core-ktx:1.7.0'
    48. implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
    49. //这个是compose依赖类,
    50. implementation 'androidx.activity:activity-compose:1.3.1'
    51. implementation "androidx.compose.ui:ui:$compose_ui_version"
    52. implementation "androidx.compose.ui:ui-tooling-preview:$compose_ui_version"
    53. implementation 'androidx.compose.material:material:1.2.0'
    54. androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_ui_version"
    55. debugImplementation "androidx.compose.ui:ui-tooling:$compose_ui_version"
    56. debugImplementation "androidx.compose.ui:ui-test-manifest:$compose_ui_version"
    57. //kapt作用我们要是用@AutoService注解,需要使用到这个。
    58. implementation 'com.google.auto.service:auto-service:1.0-rc7'
    59. kapt "com.google.auto.service:auto-service:1.0-rc7"
    60. testImplementation 'junit:junit:4.13.2'
    61. androidTestImplementation 'androidx.test.ext:junit:1.1.5'
    62. androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    63. }
    4. SPI代码:

    kotlin 代码Service接口代码:

    1. //服务接口类。
    2. interface IComposableService {}
    3. //
    4. object ComposableServiceManager {
    5. private val composableMap = HashMap>()
    6. init {
    7. ServiceLoader.load(IComposableService::class.java).forEach {
    8. composableMap[it.type] = it
    9. }
    10. }
    11. }
    12. /****/
    13. @AutoService(IComposableService::class)
    14. class TextComposable : IComposableService {
    15. }

    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.test.compose.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 支持多个实现类,您可以同时加载多个实现特定接口的类,并以迭代的方式访问它们。

     

  • 相关阅读:
    51单片机学习:LED点阵实验(显示数字)
    论文阅读——DiffusionDet
    关于个体工商户的15个财税要点,你清楚吗?
    SpringBoot系列之搭建WebSocket应用
    【Flutter小记3】Android打开前置或广角摄像头的同时打开闪光灯方案
    k8s--架构基础--云控制器管理器
    css知识学习系列(8)-每天10个知识点
    城管视频ai智能分析系统
    SqlServer 备份还原
    微服务09-Sentinel的入门
  • 原文地址:https://blog.csdn.net/shaohuazuo/article/details/134043167