ioc

package module
v0.0.0-...-705b81a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

Golang-IOC依赖注入

Golang-IOC提供了依赖注入功能,主要能力如下:

  • 支持各种结构、接口的依赖注入
  • 具备对象生命周期管理机制,可接管对象初始化和销毁

快速开始

以下示例将展示以下功能:

  1. 注册结构体
  2. 结构体初始化以及销毁方法注入
  3. 结构体自动创建
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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	// contains filtered or unexported fields
}

Container ioc容器

func NewContainer

func NewContainer() *Container

NewContainer 构造

func (*Container) Init

func (c *Container) Init(obj interface{}) error

Init 初始化对象

type IOCStarter

type IOCStarter interface {
	IOCStart() error
}

IOCStarter 依赖注入模块启动接口 初始化工作,不该出现阻塞逻辑,否则会夯住

type IOCStopper

type IOCStopper interface {
	IOCStop() error
}

IOCStopper 依赖注入模块停止接口 清理收尾工作,不该出现阻塞逻辑,否则会夯住

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL