一款服务于 Go 开发者的依赖注入框架,方便搭建任何 Go 应用。 A Golang depenedency injection framework, helps developers to build any go application.
___ ___ ____ _
|_ _| / _ \ / ___| __ _ ___ | | __ _ _ __ __ _
| | | | | | | | _____ / _` | / _ \ | | / _` | | '_ \ / _` |
| | | |_| | | |___ |_____| | (_| | | (_) | | | | (_| | | | | | | (_| |
|___| \___/ \____| \__, | \___/ |_| \__,_| |_| |_| \__, |
|___/ |___/ IOC-golang is a powerful golang dependency injection framework that provides a complete implementation of IoC containers. Its capabilities are as follows:
Supports dependency injection of any structure and interface, we also support object life cycle management mechanism
Can take over object creation, parameter injection, factory methods. Customizable object parameter source
Automatic struct descriptor codes generation capability
% go install github.com/alibaba/ioc-golang/[email protected]
% iocli
helloWe will develop a project with the following topology, This tutorial can show:
All the code the user needs to write: main.go
…The proxy wrapped layer mentioned above, is a proxy layer injected by ioc-golang by default, when developer want to inject an object to interface field, or get with interface by API. Inject to interface is recommended by us. Every object injected with proxy wrapped layer would have devops feature.
After writing, you can exec the following cli command to init go mod and generate codes. (mac may require sudo due to permissions during code generation)
% go mod init ioc-golang-demo
% export GOPROXY="https://goproxy.cn"
% go mod tidy
% go get github.com/alibaba/ioc-golang@master
% sudo iocli genIt will be generated in the current directory: zz_generated.ioc.go, developers do not need to care about this file, 'GetAppSingleton' method mentioned above is defined in generated code.
…See the file tree:
% tree
.
├── go.mod
├── go.sum
├── main.go
└── zz_generated.ioc.go
0 directories, 4 filesgo run .
Console printout:
…It shows that the injection is successful and the program runs normally.
Debug the app
Following logs can be found in console output:
[Debug] Debug server listening at :1999Open a new console, use iocli 's debug feature to list all structs with proxy layer, and their methods. Default port is 1999.
% iocli list
main.ServiceImpl1
[GetHelloString]
main.ServiceImpl2
[GetHelloString]Watch real-time param and return value. We take main.ServiceImpl 's 'GetHelloString' method as an example. The method would be called twice every 3s :
% iocli watch main.ServiceImpl1 GetHelloString
========== On Call ==========
main.ServiceImpl1.GetHelloString()
Param 1: (string) (len=8) "laurence"
========== On Response ==========
main.ServiceImpl1.GetHelloString()
Response 1: (string) (len=36) "This is ServiceImpl1, hello laurence"
========== On Call ==========
main.ServiceImpl1.GetHelloString()
Param 1: (string) (len=8) "laurence"
========== On Response ==========
main.ServiceImpl1.GetHelloString()
Response 1: (string) (len=36) "This is ServiceImpl1, hello laurence"
...// +ioc:autowire=true
The code generation tool recognizes objects marked with the +ioc:autowire=true annotation
// +ioc:autowire:type=singleton
The marker autowire model is the singletonMore code generation annotations can be viewed at iocli.
You can go to ioc-golang/example for more examples and advanced usage.
You can go to E-commercial system demo based on ioc-golang to refer to applications system on distributed scene.
IOC-golang developed by Alibaba and licensed under the Apache License (Version 2.0). See the NOTICE file for more information.
Welcome to join dingtalk group 44638289 if you are interested with the project.
If you think this project is interesting, or helpful to you, please give a star!
No open issues yet, or sync has not completed.