package
module
Version:
v0.0.0-...-705b81a
Opens a new window with list of versions in this module.
Published: Dec 7, 2023
License: Apache-2.0
Opens a new window with license information.
Imports: 3
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
Golang-IOC依赖注入
Golang-IOC提供了依赖注入功能,主要能力如下:
- 支持各种结构、接口的依赖注入
- 具备对象生命周期管理机制,可接管对象初始化和销毁
快速开始
以下示例将展示以下功能:
- 注册结构体
- 结构体初始化以及销毁方法注入
- 结构体自动创建
package main
import (
"fmt"
ioc "github.com/BrightHao/golang-ioc"
)
type Tp struct {
Name string
FootNum int
}
// 结构体初始化函数,注册后将在创建时自动调用
func (t *Tp) IOCStart() error {
t.Name = "monkey"
t.FootNum = 2
return nil
}
type Animal struct {
Age int
Color string
Tp *Tp `inject:""`
}
func main() {
// 创建最上层结构体
a := &Animal{}
// 创建IOC容器
c := ioc.NewContainer()
// 将a初始化
if err := c.Init(a); err != nil {
fmt.Println(err)
return
}
// &{0 0xc00000c090},可以看到Tp并非nil,说明已经被注入
fmt.Println(a)
// &{monkey 2},这里可以看到Tp的IOCStart函数被自动执行了
fmt.Println(a.Tp)
}
Documentation
¶
type Container struct {
}
Container ioc容器
type IOCStarter interface {
IOCStart() error
}
IOCStarter 依赖注入模块启动接口
初始化工作,不该出现阻塞逻辑,否则会夯住
type IOCStopper interface {
IOCStop() error
}
IOCStopper 依赖注入模块停止接口
清理收尾工作,不该出现阻塞逻辑,否则会夯住
Source Files
¶
Click to show internal directories.
Click to hide internal directories.