___ ___ ____ _
|_ _| / _ \ / ___| __ _ ___ | | __ _ _ __ __ _
| | | | | | | | _____ / _` | / _ \ | | / _` | | '_ \ / _` |
| | | |_| | | |___ |_____| | (_| | | (_) | | | | (_| | | | | | | (_| |
|___| \___/ \____| \__, | \___/ |_| \__,_| |_| |_| \__, |
|___/ |___/
Welcome to use ioc-golang!
[Boot] Start to load ioc-golang config
[Config] Load default config file from ../conf/ioc_golang.yaml
[Config] Load ioc-golang config file failed. open /Users/laurence/Desktop/workplace/alibaba/conf/ioc_golang.yaml: no such file or directory
The load procedure is continue
[Boot] Start to load debug
[Debug] Debug port is set to default :1999
[Boot] Start to load autowire
[Autowire Type] Found registered autowire type normal
[Autowire Struct Descriptor] Found type normal registered SD main.serviceStruct_
[Autowire Struct Descriptor] Found type normal registered SD main.app_
[Autowire Struct Descriptor] Found type normal registered SD main.serviceImpl1_
[Autowire Struct Descriptor] Found type normal registered SD main.serviceImpl2_
[Autowire Type] Found registered autowire type singleton
[Autowire Struct Descriptor] Found type singleton registered SD main.App
[Autowire Struct Descriptor] Found type singleton registered SD main.ServiceImpl1
[Autowire Struct Descriptor] Found type singleton registered SD main.ServiceImpl2
[Autowire Struct Descriptor] Found type singleton registered SD main.ServiceStruct
[Debug] Debug server listening at :1999
This is ServiceImpl1, hello laurence
This is ServiceImpl2, hello laurence
This is ServiceImpl1, hello laurence
This is ServiceStruct, hello laurence
...
可看到,依赖注入成功,程序正常运行。
调试程序
可看到打印出的日志中包含,说明 Debug 服务已经启动。
[Debug] Debug server listening at :1999
新开一个终端,使用 iocli 的调试功能,查看所有拥有代理层的结构和方法。默认端口为 1999。
% iocli list
main.ServiceImpl1
[GetHelloString]
main.ServiceImpl2
[GetHelloString]
IOC-golang:一款 GO 语言依赖注入框架
English | 中文
IOC-golang 是一款强大的 Go 语言依赖注入框架,提供了一套完善的 IoC 容器。其能力如下:
依赖注入
支持任何结构、接口的依赖注入,具备完善的对象生命周期管理机制。
可以接管对象的创建、参数注入、工厂方法、参数来源等等,并拥有可扩展性。
接口代理
基于 AOP 的思路,为由框架接管的对象提供默认的接口代理层,在面向接口编程的情景下,可以在 业务无侵入 的前提下,使用基于结构代理 AOP 层扩展的丰富运维能力。例如接口查询,参数动态监听,方法粒度链路追踪,性能瓶颈分析,分布式场景下全链路方法粒度追踪等。
代码生成能力
我们提供了代码生成工具,开发者可以通过注解的方式标注结构,从而便捷地生成结构注册代码、结构代理、结构专属接口等。
可扩展能力
支持被注入结构的扩展、自动装载模型的扩展、调试 AOP 层的扩展。
丰富的预置组件
提供覆盖主流中间件的预制对象,方便直接注入使用。
项目结构
快速开始
安装代码生成工具
依赖注入教程
我们将开发一个具有以下拓扑的工程,在本例子中,可以展示
用户所需编写的全部代码:main.go
上述所说的“代理层”,是框架为“以接口形式注入/获取”的结构体,默认封装的代理,可以基于框架提供的 AOP 扩展能力,扩展一系列运维操作。我们推荐开发者在编写代码的过程中基于接口编程,则所有对象都可拥有运维能力。
编写完毕后,当前目录执行以下命令,初始化 go mod ,拉取最新代码,生成结构注册代码。(mac 环境可能因权限原因需要sudo):
会在当前目录生成:zz_generated.ioc.go,开发者无需关心这一文件,这一文件中就包含了上面使用的 GetAppSingleton 方法
查看当前目录文件
执行程序
go run .控制台打印输出:
可看到,依赖注入成功,程序正常运行。
调试程序
可看到打印出的日志中包含,说明 Debug 服务已经启动。
新开一个终端,使用 iocli 的调试功能,查看所有拥有代理层的结构和方法。默认端口为 1999。
监听方法的参数和返回值。以监听 main.ServiceImpl 结构的 GetHelloString 方法为例,每隔三秒钟,函数被调用两次,打印参数和返回值。
注解分析
更多
中文文档
更多代码生成注解可以移步iocli.查看。
可以移步 ioc-golang/example 查看更多例子
可以参考 基于 IOC-golang 的电商系统demo 查看分布式场景下的应用系统示例
如您在使用框架的过程中遇到问题,可以参考 常见问题诊断
证书
IOC-golang developed by Alibaba and licensed under the Apache License (Version 2.0). See the NOTICE file for more information.
联系我们
感兴趣的开发者可以加入钉钉群:44638289
Star me please ⭐
如果你对这个项目本身或者实现思路感兴趣,可以给我们一颗 ⭐