鸿蒙开源组件——实用型轻量级依赖注入框架

jacksky
发布于 2021-11-12 17:25
浏览
0收藏

KOIN

本项目是基于开源项目InsertKoinIO/koin进行适用OHOS的移植和开发的, 可通过github地址https://github.com/InsertKoinIO/koin 追踪到原项目

项目介绍

koin是一个实用型轻量级依赖注入框架

项目名称:KOIN

所属系列:OHOS的第三方组件适配移植

功能:koin是一个实用型轻量级依赖注入框架

项目移植状态:未移植部分请参考changelog

调用差异:无

原项目GitHub地址:https://github.com/InsertKoinIO/koin

安装教程

在根目录的build.gradle文件中添加mavenCentral()

   // Top-level build file where you can add configuration options common to all sub-projects/modules.
   ...
   buildscript {
       repositories {
           ...
           mavenCentral()
       }
       ...
   }
   
   allprojects {
       repositories {
           ...
           mavenCentral()
       }
   }

在你的项目中添加依赖

1.如果只使用koin相关api,则可以只引入核心库

dependencies {
    implementation 'com.gitee.ts_ohos:koin_core:1.0.1'
}

2.如果要使用hos的相关特性,则可以引入以下内容

dependencies {
    implementation 'com.gitee.ts_ohos:koin_hos:1.0.1'
}

使用说明

Java

下面的示例展示了通过koin注册和解析模块

在该示例中:

  • HelloMessageData - 数据持有者
  • HelloService - 使用和展示来自HelloMessageData中的数据
  • HelloApplication - 解析和使用HelloService

Data holder

先创建HelloMessageData用于存储需要展示的数据

public class HelloMessageData {

    public String message;

    public HelloMessageData(String message) {
        this.message = message;
    }

    public HelloMessageData() {
        this.message = "Hello Koin!";
    }
}

Service

创建用于展示HelloMessageData数据的类。HelloServiceImpl类实现了接口HelloService:

/**
 * Hello Service - interface
 */
public interface HelloService {

    String hello();

}

/**
 * Hello Service Impl
 * Will use HelloMessageData data
 */
public class HelloServiceImpl implements HelloService {

    private HelloMessageData helloMessageData;

    public HelloServiceImpl(HelloMessageData helloMessageData) {
        this.helloMessageData = helloMessageData;
    }

    @Override
    public String hello() {
        return "Hey, " + helloMessageData.message;
    }
}

Application

要运行HelloService组件,需要创建一个运行时组件。 现在创建HelloApplication实现了KoinComponent接口。 稍后可以使用inject来解析需要的组件

/**
 * HelloApplication - Application Class
 * use HelloService
 */
public class HelloApplication implements KoinComponent {

    public HelloService helloService;

    public HelloApplication() {
        try {
            // Inject HelloService
            helloService = inject(HelloService.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    // display our data
    public void sayHello() {
        System.out.println(helloService.hello());
    }
}

依赖声明

现在,通过Koin的module,我们将HelloMessageDataHelloService关联起来:

public class HelloModule {

    public static final Module helloModule = org.koin.dsl.Module.module(new ModuleDeclaration() {
        @Override
        public void invoke(Module module) {
            try {
                module.single(HelloMessageData.class, new Definition<HelloMessageData>() {
                    @Override
                    public HelloMessageData invoke(Scope scope, DefinitionParameters parameters) {
                        return new HelloMessageData();
                    }
                });

                module.single(HelloService.class, new Definition<HelloService>() {
                    @Override
                    public HelloService invoke(Scope scope, DefinitionParameters parameters) {
                        try {
                            return new HelloServiceImpl(scope.get(HelloMessageData.class));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                });

            } catch (DefinitionOverrideException e) {
                e.printStackTrace();
            }
        }
    });

}

这个示例中的两个组件都是以single模式声明,即两个组件都是单例模式。

  • module.single(HelloMessageData.class,...):声明一个HelloMessageData的单例模式的实例
  • module.single(HelloService.class,...):声明一个HelloService的单例模式,其实例为HelloServiceImpl ,并将HelloMessageData注入其中

验证结果

我们可以在main中去使用刚才注册的模块:

public static void main(String[] args) {
        try {

            //start koin
            DefaultContextExt.startKoin(new org.koin.dsl.KoinAppDeclaration() {
                @Override
                public void invoke(KoinApplication koinApplication) {

                    // use Koin logger
                    koinApplication.printLogger();

                    try {
                        // declare modules
                        koinApplication.modules(HelloModule.helloModule);
                    } catch (DefinitionOverrideException e) {
                        e.printStackTrace();
                    }
                }
            });

            //use it
            new HelloApplication().sayHello();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

更多示例可以参考

  • examples
  • quickstart_hos
  • hos_samples

移植版本

Tag 3.0.2

版本迭代

  • v1.0.1 fix bugs
  • v1.0.0 ohos首次移植版本

License

/*
 * Copyright 2017-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 

 

koin-ohos-master (1).zip 394.66K 10次下载
已于2021-11-12 17:25:59修改
收藏
回复
举报
回复
    相关推荐