Coder Social home page Coder Social logo

catinject's Introduction

CatInject

一个自动注入依赖的框架

导入

mavenCentral 版本: Maven Central
mavenCentral 发布需要一定时间, 如果发现最新版本不能用, 建议稍等4小时左右

  1. 在项目根 build.gradle:
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("io.github.zsqw123:cat-inject-plugin:$version")
    }
}
  1. 在需要用到注入的模块的 build.gradle:
plugins {
    id "cat-inject"
}

dependencies {
    implementation "io.github.zsqw123:cat-inject-lib:$version"
}

使用

在需要自动注入的接口上添加注解 @CatInject

@CatInject
interface IUser {
    fun getName(): String
    fun getAge(): Int
}

在任意位置(任意内外部依赖均可)实现这个接口:

class UserImpl : IUser {
    override fun getName(): String = "my-injected-user"
    override fun getAge(): Int = 1
}

class UserImpl2 : IUser {
    override fun getName(): String = "my-injected-user2"
    override fun getAge(): Int = 2
}

然后在导入了依赖的库里面就可以自动注入依赖了!

val randomUser = CatInjects.getInstance<IUser>() // 随便获取一个实例
val age2user = CatInjects.getAllInstances<IUser>() // 获取全部实例, 筛选所需实例
	.first { it.getAge() == 2 }

示例:zsqw123/CatInjectSample

具体例子: zsqw123/CatInjectSample

Thanks

Gradle Transform + ASM 探索 (juejin.cn)

App流畅度优化:利用字节码插桩实现一个快速排查高耗时方法的工具

xiazunyang/discovery

bytedance/ByteX: 字节码插件开发平台

catinject's People

Contributors

zsqw123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

catinject's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.