{"id":13461362,"url":"https://github.com/alibaba/IOC-golang","last_synced_at":"2025-03-24T22:34:35.717Z","repository":{"id":37005953,"uuid":"493181533","full_name":"alibaba/IOC-golang","owner":"alibaba","description":"一款服务于 Go 开发者的依赖注入框架，方便搭建任何 Go 应用。 A Golang depenedency injection framework, helps developers to build any go application.","archived":false,"fork":false,"pushed_at":"2023-06-13T05:31:26.000Z","size":1280,"stargazers_count":1208,"open_issues_count":26,"forks_count":120,"subscribers_count":22,"default_branch":"master","last_synced_at":"2024-10-16T01:42:08.750Z","etag":null,"topics":["annotation","aop","debug","dependency-injection","golang","middleware","monkey","sdk","tag"],"latest_commit_sha":null,"homepage":"https://ioc-golang.github.io","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alibaba.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2022-05-17T09:25:14.000Z","updated_at":"2024-10-04T10:40:44.000Z","dependencies_parsed_at":"2024-01-06T14:15:08.867Z","dependency_job_id":null,"html_url":"https://github.com/alibaba/IOC-golang","commit_stats":{"total_commits":106,"total_committers":6,"mean_commits":"17.666666666666668","dds":"0.28301886792452835","last_synced_commit":"1895e3979b7486fcdd6636561b9a2535c6c25f83"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2FIOC-golang","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2FIOC-golang/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2FIOC-golang/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alibaba%2FIOC-golang/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alibaba","download_url":"https://codeload.github.com/alibaba/IOC-golang/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222019229,"owners_count":16917276,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["annotation","aop","debug","dependency-injection","golang","middleware","monkey","sdk","tag"],"created_at":"2024-07-31T11:00:35.846Z","updated_at":"2024-10-29T08:30:35.606Z","avatar_url":"https://github.com/alibaba.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# IOC-golang: A golang dependency injection framework\n\n```\n  ___    ___     ____                           _                         \n |_ _|  / _ \\   / ___|           __ _    ___   | |   __ _   _ __     __ _ \n  | |  | | | | | |      _____   / _` |  / _ \\  | |  / _` | | '_ \\   / _` |\n  | |  | |_| | | |___  |_____| | (_| | | (_) | | | | (_| | | | | | | (_| |\n |___|  \\___/   \\____|          \\__, |  \\___/  |_|  \\__,_| |_| |_|  \\__, |\n                                |___/                               |___/ \n```\n\n[![IOC-golang CI](https://github.com/alibaba/IOC-golang/actions/workflows/github-actions.yml/badge.svg)](https://github.com/alibaba/IOC-golang/actions/workflows/github-actions.yml)\n[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)\n\n### English | [中文](./README_CN.md)\n\n![demo gif](https://raw.githubusercontent.com/ioc-golang/ioc-golang-website/main/resources/video/ioc-golang-demo.gif)\n\nIOC-golang is a powerful golang dependency injection framework that provides a complete implementation of IoC containers. Its capabilities are as follows:\n\n[Dependency Injection](https://ioc-golang.github.io/docs/concept/ioc/di/)\n\n   - Supports dependency injection of any structure and interface, we also support object life cycle management mechanism\n\n   - Can take over object creation, parameter injection, factory methods. Customizable object parameter source\n\n[Struct Proxy](https://ioc-golang.github.io/docs/concept/aop/proxy/)\n\n   - Based on the idea of AOP, we provide struct proxy layer for all struct registered to ioc-golang. In the scene of interface oriented development, we can use many devlops features based on the extenablility of this proxy AOP layer. Such as interface listing, param value watching, method level tracing, performance badpoint analysis, fault injection, method level tracing in distributed system and so on.\n\n[Automatic struct descriptor codes generation capability](https://ioc-golang.github.io/docs/reference/iocli/#%E6%B3%A8%E8%A7%A3%E4%B8%8E%E4%BB%A3%E7%A0%81%E7%94%9F%E6%88%90)\n\n   - We provide a code generation tool, and developers can annotate the structure through annotations, so as to easily generate structure registration code.\n\n[Scalability](./extension)\n\n   - Support the extension of struct to be injected, the extension of autowire model, and the extension of the debug AOP layer.\n\n[Many pre-defined components](./example)\n\n   - Provides pre-defined objects and middleware sdk for injection directly.\n\n## Project Structure\n\n- **aop:** Debug module: Provide debugging API, provide debugging injection layer basic  implementation and extendable API.\n- **autowire:** Provides two basic injection models: singleton model and multi-instance model\n- **config:** Configuration loading module, responsible for parsing ion-golang's configuration files\n- **extension:** Component extension directory: Provides preset implementation structures based on various domain. Such as database, cache, pubs.\n- **example:** example repository\n- **iocli:** code generation/program debugging tool\n\n## Quick start\n\n### Install code generation tools\n\n```shell\n% go install github.com/alibaba/ioc-golang/iocli@v1.0.3\n% iocli\nhello\n````\n\n### Dependency Injection Tutorial\n\nWe will develop a project with the following topology, This tutorial can show:\n\n1. Registry codes generation\n2. Interface injection\n3. Struct pointer injection\n4. Get object by API\n5. Debug capability, list interface, implementations and methods; watch real-time param and return value.\n\n![ioc-golang-quickstart-structure](https://raw.githubusercontent.com/ioc-golang/ioc-golang-website/main/resources/img/ioc-golang-quickstart-structure.png)\n\n\nAll the code the user needs to write: main.go\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"time\"\n\n\t\"github.com/alibaba/ioc-golang\"\n)\n\n// +ioc:autowire=true\n// +ioc:autowire:type=singleton\n\ntype App struct {\n\t// inject main.ServiceImpl1 pointer to Service interface with proxy wrapper\n\tServiceImpl1 Service `singleton:\"main.ServiceImpl1\"`\n\n\t// inject main.ServiceImpl2 pointer to Service interface with proxy wrapper\n\tServiceImpl2 Service `singleton:\"main.ServiceImpl2\"`\n\n\t// inject ServiceImpl1 pointer to Service1 's own interface with proxy wrapper\n\t// this interface belongs to ServiceImpl1, there is no need to mark 'main.ServiceImpl1' in tag\n\tService1OwnInterface ServiceImpl1IOCInterface `singleton:\"\"`\n\n\t// inject ServiceStruct struct pointer\n\tServiceStruct *ServiceStruct `singleton:\"\"`\n}\n\nfunc (a *App) Run() {\n\tfor {\n\t\ttime.Sleep(time.Second * 3)\n\t\tfmt.Println(a.ServiceImpl1.GetHelloString(\"laurence\"))\n\t\tfmt.Println(a.ServiceImpl2.GetHelloString(\"laurence\"))\n\n\t\tfmt.Println(a.Service1OwnInterface.GetHelloString(\"laurence\"))\n\t\t\n\t\tfmt.Println(a.ServiceStruct.GetString(\"laurence\"))\n\t}\n}\n\ntype Service interface {\n\tGetHelloString(string) string\n}\n\n// +ioc:autowire=true\n// +ioc:autowire:type=singleton\n\ntype ServiceImpl1 struct {\n}\n\nfunc (s *ServiceImpl1) GetHelloString(name string) string {\n\treturn fmt.Sprintf(\"This is ServiceImpl1, hello %s\", name)\n}\n\n// +ioc:autowire=true\n// +ioc:autowire:type=singleton\n\ntype ServiceImpl2 struct {\n}\n\nfunc (s *ServiceImpl2) GetHelloString(name string) string {\n\treturn fmt.Sprintf(\"This is ServiceImpl2, hello %s\", name)\n}\n\n// +ioc:autowire=true\n// +ioc:autowire:type=singleton\n\ntype ServiceStruct struct {\n}\n\nfunc (s *ServiceStruct) GetString(name string) string {\n\treturn fmt.Sprintf(\"This is ServiceStruct, hello %s\", name)\n}\n\nfunc main() {\n\t// start to load all structs\n\tif err := ioc.Load(); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Get Struct\n\tapp, err := GetAppSingleton()\n\tif err != nil {\n\t\tpanic(err)\n\t}\n\tapp.Run()\n}\n\n\n```\nThe 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.\n\nAfter 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)\n\n```bash\n% go mod init ioc-golang-demo\n% export GOPROXY=\"https://goproxy.cn\"\n% go mod tidy\n% go get github.com/alibaba/ioc-golang@master\n% sudo iocli gen\n````\n\nIt 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.\n\n```go\n//go:build !ignore_autogenerated\n// +build !ignore_autogenerated\n\n// Code generated by iocli\n\npackage main\n\nimport (\n        autowire \"github.com/alibaba/ioc-golang/autowire\"\n        normal \"github.com/alibaba/ioc-golang/autowire/normal\"\n        \"github.com/alibaba/ioc-golang/autowire/singleton\"\n        util \"github.com/alibaba/ioc-golang/autowire/util\"\n)\n\nfunc init() {\n        normal.RegisterStructDescriptor(\u0026autowire.StructDescriptor{\n                Factory: func() interface{} {\n                        return \u0026app_{}\n                },\n        })\n        singleton.RegisterStructDescriptor(\u0026autowire.StructDescriptor{\n                Factory: func() interface{} {\n                        return \u0026App{}\n                },\n        })\n  ...\nfunc GetServiceStructIOCInterface() (ServiceStructIOCInterface, error) {\n        i, err := singleton.GetImplWithProxy(util.GetSDIDByStructPtr(new(ServiceStruct)), nil)\n        if err != nil {\n                return nil, err\n        }\n        impl := i.(ServiceStructIOCInterface)\n        return impl, nil\n}\n\n\n```\n\nSee the file tree:\n\n```bash\n% tree\n.\n├── go.mod\n├── go.sum\n├── main.go\n└── zz_generated.ioc.go\n\n0 directories, 4 files\n````\n\n#### Execute program\n\n`go run .`\n\nConsole printout:\n\n```sh\n  ___    ___     ____                           _                         \n |_ _|  / _ \\   / ___|           __ _    ___   | |   __ _   _ __     __ _ \n  | |  | | | | | |      _____   / _` |  / _ \\  | |  / _` | | '_ \\   / _` |\n  | |  | |_| | | |___  |_____| | (_| | | (_) | | | | (_| | | | | | | (_| |\n |___|  \\___/   \\____|          \\__, |  \\___/  |_|  \\__,_| |_| |_|  \\__, |\n                                |___/                               |___/ \nWelcome to use ioc-golang!\n[Boot] Start to load ioc-golang config\n[Config] Load default config file from ../conf/ioc_golang.yaml\n[Config] Load ioc-golang config file failed. open /Users/laurence/Desktop/workplace/alibaba/conf/ioc_golang.yaml: no such file or directory\n The load procedure is continue\n[Boot] Start to load debug\n[Debug] Debug port is set to default :1999\n[Boot] Start to load autowire\n[Autowire Type] Found registered autowire type normal\n[Autowire Struct Descriptor] Found type normal registered SD main.serviceStruct_\n[Autowire Struct Descriptor] Found type normal registered SD main.app_\n[Autowire Struct Descriptor] Found type normal registered SD main.serviceImpl1_\n[Autowire Struct Descriptor] Found type normal registered SD main.serviceImpl2_\n[Autowire Type] Found registered autowire type singleton\n[Autowire Struct Descriptor] Found type singleton registered SD main.App\n[Autowire Struct Descriptor] Found type singleton registered SD main.ServiceImpl1\n[Autowire Struct Descriptor] Found type singleton registered SD main.ServiceImpl2\n[Autowire Struct Descriptor] Found type singleton registered SD main.ServiceStruct\n[Debug] Debug server listening at :1999\nThis is ServiceImpl1, hello laurence\nThis is ServiceImpl2, hello laurence\nThis is ServiceImpl1, hello laurence\nThis is ServiceStruct, hello laurence\n...\n```\n\nIt shows that the injection is successful and the program runs normally.\n\n**Debug the app**\n\nFollowing logs can be found in console output:\n\n```bash\n[Debug] Debug server listening at :1999\n```\n\nOpen a new console, use iocli 's debug feature to list all structs with proxy layer, and their methods. Default port is 1999.\n\n```\n% iocli list\nmain.ServiceImpl1\n[GetHelloString]\n\nmain.ServiceImpl2\n[GetHelloString]\n```\n\nWatch real-time param and return value. We take  main.ServiceImpl 's 'GetHelloString' method as an example. The method would be called twice every 3s :\n\n```bash\n% iocli watch main.ServiceImpl1 GetHelloString\n========== On Call ==========\nmain.ServiceImpl1.GetHelloString()\nParam 1: (string) (len=8) \"laurence\"\n\n========== On Response ==========\nmain.ServiceImpl1.GetHelloString()\nResponse 1: (string) (len=36) \"This is ServiceImpl1, hello laurence\"\n\n========== On Call ==========\nmain.ServiceImpl1.GetHelloString()\nParam 1: (string) (len=8) \"laurence\"\n\n========== On Response ==========\nmain.ServiceImpl1.GetHelloString()\nResponse 1: (string) (len=36) \"This is ServiceImpl1, hello laurence\"\n...\n```\n\n\n\n### Annotation Analysis\n\n````go\n// +ioc:autowire=true\nThe code generation tool recognizes objects marked with the +ioc:autowire=true annotation\n\n// +ioc:autowire:type=singleton\nThe marker autowire model is the singleton\n````\n\n###  More\n\n[Docs](https://ioc-golang.github.io/cn)\n\nMore code generation annotations can be viewed at [iocli](https://github.com/alibaba/IOC-golang/tree/master/iocli).\n\nYou can go to [ioc-golang/example](https://github.com/alibaba/IOC-golang/tree/master/example) for more examples and advanced usage.\n\nYou can go to [E-commercial system demo based on ioc-golang](https://github.com/ioc-golang/shopping-system) to refer to applications system on distributed scene.\n\n### License\n\nIOC-golang developed by Alibaba and licensed under the Apache License (Version 2.0).\nSee the NOTICE file for more information.\n\n### Connect with us\n\nWelcome to join dingtalk group 44638289 if you are interested with the project.\n\n\u003cdiv align=\"center\"\u003e\n\t\u003cimg src=\"https://github.com/ioc-golang/ioc-golang-website/blob/main/resources/img/dingtalk_group.png?raw=true\" width=\"30%\"\u003e\n\u003c/div\u003e\n\n### Star me please ⭐\n\nIf you think this project is interesting, or helpful to you, please give a star!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falibaba%2FIOC-golang","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falibaba%2FIOC-golang","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falibaba%2FIOC-golang/lists"}